JavaScript Form Validation
HTML form validation can be done by JavaScript.
Email Field validation
Validate any Email Id Pattern
Using Regular Expression match the pattern of email id.
Validate the email id pattern.for example xyz@abc.def
Example
<script>>
function validateForm()
{
var email = document.getElementById('eid');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;//Validate Email Id
if (!filter.test(email.value)) {
alert('Please provide your Valide Email address');
email.focus;
return false;}
else{
var e=document.getElementById('eid').value;
document.write("Your Email Id is:"+ e);
}
}
</script>
<form name="myform" onsubmit="return validateForm()" method="post">
Enter your Mail Id:&l;tinput type="text" name="email" id="eid" />
<input type="submit" value="Submit">
</form>
Run Example>>
Validate only Gmail id
Using Regular Expression match the pattern of Gmail email id.
Example
<script>>
function validateForm()
{
var email = document.getElementById('eid');
var filter = /^([a-zA-Z0-9_])+\@gmail.com/;//Validate gmail email id
if (!filter.test(email.value)) {
alert('Please provide your Valide Gmail address');
email.focus;
return false;}
else{
var e=document.getElementById('eid').value;
document.write("Your Email Id is:"+ e);
}
}
</script>
<form name="myform" onsubmit="return validateForm()" method="post">
Enter your Mail Id:&l;tinput type="text" name="email" id="eid" />
<input type="submit" value="Submit">
</form>
Run Example>>