Javascript Form Validation
Form Validation using JavaScript
When form submitted, control is given to JavaScript. JavaScript validates from data, in case of any errors it notifies the user and receive correct data from user. Ex: Password minimum length is 8 characters. If user enter a password that is less than 8 char. It sends alert message to the user until he gives proper data. Once all the data validated, then JavaScript will send it to sever. This will not only save network resources from transmitting unnecessary data over the network, but also save server valuable time.
Below examples shows you how JavaScript validates the form data.
JS Home
index.html
<html>
<head>
<title> e-trainings demo on Registration form validation</title>
<script language="JavaScript">
function form_load(form)
{
document.forms [0].elements[0].focus();
}
function validation()
{
//Validation for Email ID
em = document.reg.emailid.value;
pos1 = em.indexOf("@");
pos = em.indexOf(" ");
if(document.reg.fname.value == "")
{
alert("Enter the First Name.");
document.reg.fname.focus();
return false;
}
if(document.reg.lname.value == "")
{
alert("Enter the Last Name.");
document.reg.lname.focus();
return false;
}
if(document.reg.gender.value == "")
{
alert("Select Gender");
document.reg.gender.focus();
return false;
}
if(em == "")
{
document.reg.emailid.focus();
alert("Email cannot be blank");
return false;
}
else if(em.length<8)
{
document.reg.emailid.focus();
alert("Email length cannot be less than 8 characters");
return false;
}
else if(em.indexOf("@")==-1)
{
document.reg.emailid.focus();
alert("The Email Address must contain '@' sign");
return false;
}
else if(pos1<1)
{
document.reg.emailid.focus();
alert("Email address cannot start with '@' sign");
return false;
}
else if(em.indexOf(".")==-1)
{
document.reg.emailid.focus();
alert("The Email Address must contain '.' sign");
return false;
}
else if(pos != -1)
{
document.reg.emailid.focus();
alert("The Email Address cannot contain spaces");
return false;
}
if(document.reg.password.value == "")
{
alert("Enter the Password.");
document.reg.password.focus();
return false;
}
if(document.reg.password.value.length<8 ||
document.reg.password.value.length>15 )
{
alert("Minimum charaters for password is 8 and Maximum character is 15");
document.reg.password.focus();
return false;
}
if(document.reg.cpassword.value == "")
{
alert("Please Confirm the Password.");
document.reg.cpassword.focus();
return false;
}
if(document.reg.password.value != document.reg.cpassword.value)
{
alert("Password not matching.");
return false;
}
if(document.reg.contact.value == "")
{
alert("Enter the Contact.");
document.reg.contact.focus();
return false;
}
if(document.reg.contact.value.length < 10 ||
document.reg.contact.value.length>11)
{
alert("Enter proper Contact.");
return false;
}
}
function form_load(form)
{
document.forms [0].elements[0].focus();
}
</script>
</head>
<body onLoad="form_load(this)">
<form id="reg" name="reg" method="post" action="" onsubmit="return
validation()">
<table width="478" border="0">
<tr>
<td width="171" height="55">First Name:</td>
<td width="300"><input class="right" name="fname" type="text"
id="fname" size="50" /></td>
</tr>
<tr>
<td height="43">Last Name:</td>
<td><input name="lname" type="text" id="lname" size="50" /></td>
</tr>
<tr>
<td height="40">Gender:
</td>
<td>
<input name="gender" type="radio" id="gender" value="Male"
checked="checked" />
Male
<input name="gender" type="radio" id="gender" value="Female"/>
Female </td>
</tr>
<tr>
<td height="49">Email-ID:</td>
<td><input name="emailid" type="text" id="emailid" size="50" /></td>
</tr>
<tr>
<td height="49">Password:</td>
<td><input name="password" type="password" id="password" size="50"
/></td>
</tr>
<tr>
<td height="43">Confirm Password:</td>
<td><input name="cpassword" type="password" id="cpassword" size="50"
/></td>
</tr>
<tr>
<td height="43">Contact:</td>
<td><input name="contact" type="text" id="contact" size="50" /></td>
</tr>
<tr>
<td height="39" colspan="2" align="center"><input type="submit"
name="submit" id="submit" value="Register" /></td>
</tr>
</table>
</form>
</body>
</html>
Comments
Post a Comment