1

I am validating the html form using javascript. But it was not working

In scripting i done some basic validation.

Below is my html form and javascript validation

please help me where i done mistake

Javascript validation

function register()
    {
        var fname = document.getElementById('fname').value;
        var lname = document.getElementById('lname').value;
        var email = document.getElementById('email').value;
        var contact = document.getElementById('contact').value;
        var pass = document.getElementById('pass').value;      


        var alpha = /^[a-zA-Z\s-, ]+$/;  

        if (fname.value == "") {
        alert('Please enter First Name');
        return false;
         }
        else if (!fname.value.match(alpha)) {
        alert('First Name allows only Alphabets');       
        return false;
         }
        else 
         {
         return true;
         }

    }

HTML Form

<form role="form" method="post" action="" onSubmit="return register();">
    <div class="form-group">
      <label>First Name <span class="mandatory"> *</span></label>
      <input type="text" class="form-control" name="fname" id="fname" placeholder="Enter Your First Name" value="" required>
    </div>
    <div class="form-group">
      <label>Last Name <span class="mandatory"> *</span></label>
      <input type="text" class="form-control" id="lname" name="lname" placeholder="Enter Your Last Name" value="" required>
    </div>
    <div class="form-group">
      <label for="emmail_register">Email address <span class="mandatory"> *</span></label>
      <input id="emmail_register" id="email" name="email" class="form-control" type="text" placeholder="Enter Your Email id" value="" required>
    </div>
    <div class="form-group">
       <label>Contact No. <span class="mandatory"> *</span></label>
       <input type="text" id="contact" name="contact" class="form-control" value="" placeholder="Enter Contact No." required>
    </div>
    <div class="form-group">
       <label>Password <span class="mandatory"> *</span></label>
       <input type="password" id="pass" name="pass" class="form-control" placeholder="Enter your password" value="" required>
    </div>       
    <div class="form-group">
       <input type="submit" onclick="return register();" name="user_register" class="btn btn-primary" value="Create Account">
    </div>          
</form>
1

1 Answer 1

2

Not need to get value again using fname.value in if/else condition. Try like this..

if (fname== "") {
  alert('Please enter First Name');
  return false;
}
else if (!fname.match(alpha)) {
  alert('First Name allows only Alphabets');       
  return false;
}
else{
  return true;
}
Sign up to request clarification or add additional context in comments.

Comments

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.