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>