0

I am new to javascript. I am trying to figure out why my javascript function is not being called. I am trying to add nameVlidation for name field and other validation for each text input. But my name validation itself is not working.

Javascript call (formValidation.js)

'use strict';

var nameValidation = formValidation().nameValidation();

document.getElementById('submit').addEventListener('click',nameValidation);

var formValidation = function () {

return {
    nameValidation: function() {

        this.name = document.forms["contact"]["name"].value;

        if(this.name=="") {
            alert("Enter name, required");
            return false;
        }else if(this.name==[0-9]) {
            alert("Only alphabets");
        }

        return true;
    },

    addressValidation: function() {

    }
  }
};

Html

    <form name="contact" action="#" method="post" enctype="text/plain">
<input type="text" name="name" placeholder="NAME"></br>
<input type="text" name="email" placeholder="EMAIL" required></br>
<input type="text" name="phoneNumber" placeholder="PHONE-NUMBER"></br> 
 <input type="text-box" name="message" placeholder="MESSAGE"></br>
<button id="submit" class="btn btn-primary" type="submit"><i class="fa fa-paper-plane">Submit</i></button></br>
  </form>
<script src="js/formValidation/formValidation.js"></script>

I am not sure what is wrong with it.

2 Answers 2

1
     'use strict';
//Rest of your code goes here
        document.getElementById('submit').addEventListener('click',nameValidation);

Because of function hoisting in Javscript.By default, JavaScript moves all the function declarations to the top of the current scope. This is called function hoisting. This is the reason your nameValidation function is not called in your current code.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem you are facing is due to a concept in Javascript called, Hoisting

You need to declare formValidation at the top before calling it from anywhere else, as you are using the function expression form

'use strict';

var formValidation = function () {

return {
    nameValidation: function() {

        this.name = document.forms["contact"]["name"].value;

        if(this.name=="") {
            alert("Enter name, required");
            return false;
        }else if(this.name==[0-9]) {
            alert("Only alphabets");
        }

        return true;
    },

    addressValidation: function() {

    }
  }
};

var nameValidation = formValidation().nameValidation();

document.getElementById('submit').addEventListener('click',nameValidation);

PS - JSBin Demo

2 Comments

Thanks for the links. I understood why it did not work for function expression. :)
@user3369719 Glad it helped you :) forgot to add the demo link.. Added that to the answer as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.