Lets see an illustration how we can validate age and email id through javascript
In this example,we are taking inputs from user in age(textbox) and Email (Textbox). By using functions of javascript we can validate both the values.
function ValidateAge()
{
//age numeric validation
if(document.getElementById("txtempage").value=="")
{
alert("Enter the age")
document.getElementById("txtempage").focus();
return false;
}
var digits=RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
var digitsid=document.getElementById("txtempage").value;
var digitsArray = digitsid.match(digits);
var temp;
if (digitsArray == null)
{
alert("Your age is incorrect");
document.getElementById("txtempage").focus();
return false;
}
//Email Validation
if(document.getElementById("txtemail").value=="")
{
alert("Email id can not be blank");
document.getElementById("txtemail").focus();
return false;
}
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById("txtemail").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("txtemail").focus();
return false;
}
}
0 Comment(s)