JavaScript Form Validation Code
<html>
<head>
<title>Javascript All At Once Validation</title>
<script>
function validate(){
var eid=document.getElementById("eid");
var ename=document.getElementById("ename");
var epass=document.getElementById("epass");
var ecpass=document.getElementById("ecpass");
var eadd=document.getElementById("eadd");
var ecity=document.getElementById("ecity");
var epin=document.getElementById("epin");
var eemail=document.getElementById("eemail");
if(notEmpty(eid,"Employee ID Should Not Be Blank"))
{
if(onlyAlpha(ename,"Employee Name Should Contain Only Alphabet"))
{
if(lenRestrict(epass,6,10))
{
if(compVal(epass,ecpass,"Employee Password and Confime Should Same"))
{
if(onlyAlphaNum(eadd,"Employee Address Only Contain Only Alphabet,digits,hypen,comma.."))
{
if(mustSelect(ecity,"Please Select One Employee City"))
{
if(onlyDigits(epin,"Employee Pin Should Contain Only Digits"))
{
if(emailValid(eemail,"Invalid Employee Email"))
{
return true;
}
}
}
}
}
}
}
}
return false;
}
function notEmpty(elem,msg){
if(elem.value.length==0){
alert(msg);
elem.focus();
return false;
}
return true;
}
function onlyAlpha(elem,msg){
var exp=/^[a-z A-Z]+$/;
if(!elem.value.match(exp)){
alert(msg);
elem.focus();
return false;
}
return true;
}
function lenRestrict(elem,min,max){
if(!(elem.value.length>=min && elem.value.length<=max)){
alert("Password Between " + min + " and " + max + " Chars");
elem.focus();
return false;
}
return true;
}
function compVal(elem1,elem2,msg)
{
if(elem1.value!=elem2.value)
{
alert(msg);
elem2.focus();
return false;
}
return true;
}
function onlyAlphaNum(elem,msg){
var exp=/^[a-zA-Z0-9 ,-:\\]+$/;
if(!elem.value.match(exp)){
alert(msg);
elem.focus();
return false;
}
return true;
}
function mustSelect(elem,msg){
if(elem.value=="Select One"){
alert(msg);
elem.focus();
return false;
}
return true;
}
function onlyDigits(elem,msg){
var exp=/^[0-9]+$/;
if(!elem.value.match(exp)){
alert(msg);
elem.focus();
return false;
}
return true;
}
function emailValid(elem,msg){
var exp=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(!elem.value.match(exp)){
alert(msg);
elem.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form onsubmit="return validate()">
<table border=1 align=center>
<tr>
<th colspan=2>Employee Inforation</th>
</tr>
<tr>
<td>Enter Employee ID</td>
<td><input type=text id=eid></td>
</tr>
<tr>
<td>Enter Employee Name</td>
<td><input type=text id=ename></td>
</tr>
<tr>
<td>Enter Employee Password</td>
<td><input type=password id=epass></td>
</tr>
<tr>
<td>Enter Employee Confirm Password</td>
<td><input type=password id=ecpass></td>
</tr>
<tr>
<td>Enter Employee Address</td>
<td><input type=text id=eadd></td>
</tr>
<tr>
<td>Select Employee City</td>
<td>
<select id=ecity>
<option value="Select One">Select One</option>
<option value="bbsr">Bhubaneswar</option>
<option value="ctc">Cuttack</option>
<option value="bls">Balasore</option>
<option value="bdk">Bhadrak</option>
</select>
</td>
</tr>
<tr>
<td>Enter Employee Pincode</td>
<td><input type=text id=epin></td>
</tr>
<tr>
<td>Enter Employee Email</td>
<td><input type=text id=eemail></td>
</tr>
<tr>
<td></td>
<td><input type=Submit value=Validate ></td>
</tr>
</table>
</form>
</body>
</html>
Comments
Post a Comment