Saturday, 7 September 2013

Form alidation

javascript::form validation
Member Registration
User Name:
password:
cpassword:
address:
City:
States:
Zip Code:
Interests: Movies Sports
Gender: Male Female
Upload Picture:
________

<html>
<head>
<title>javascript::form validation</title>
<script>
function validation()
{
//Username validation
var userval=document.regform.username.value;

//Check null or not
if(userval=="" || userval==null)
{
alert("Username should not be blank");
document.regform.username.focus();
return false;
}

//Check minimum length
if(userval.length<=3)
{
alert("Username should be minimum 4 characters");
document.regform.username.focus();
return false;
}

//Check special charactes
for(var i=0;i<userval.length;i++)
{
var chr=userval.charAt(i);
if((chr>=0 && chr<=9)||(chr>='a' && chr<='z')||(chr>='A'&&chr<='Z'))
{

}
else
{
alert("Username should not be contain special characters");
document.regform.username.focus();
document.regform.username.value="";
return false;
}
}

//Password validation
var passval=document.regform.userpass.value;

//Check null or not
if(passval=="" || passval==null)
{
alert("Password should not be blank");
document.regform.userpass.focus();
return false;
}

//Check minimum length
if(passval.length<=7)
{
alert("Password should be minimum 8 characters");
document.regform.userpass.focus();
return false;
}

//Confirm password validation
var cpassval=document.regform.cpass.value;

if(cpassval!=passval)
{
alert("Passwords should be match");
document.regform.cpass.focus();
return false;
}


//Address validation
var addr=document.regform.address.value;

//Check null or not
if(addr=="" || addr==null)
{
alert("Address should not be blank");
document.regform.address.focus();
return false;
}

//City validation
var cityval=document.regform.city.value;

//Check null or not
if(cityval=="" || cityval==null)
{
alert("City should not be blank");
document.regform.city.focus();
return false;
}

if(!isNaN(cityval))
{
alert("City should not be contain numeric values");
document.regform.city.focus();
return false;
}

//State validation
var state=document.regform.state.value;

//Check null or not
if(state=="" || state==null)
{
alert("Please select state");
document.regform.state.focus();
return false;
}


//Zipcode validation
var zipval=document.regform.zip.value;

//Check null or not
if(zipval=="" || zipval==null)
{
alert("Zip code should not be blank");
document.regform.zip.focus();
return false;
}

if(isNaN(zipval) || zipval.length!=6)
{
alert("Zip code should not be contain alphabetic values and length should be 6");
document.regform.zip.focus();
return false;
}
//Zipcode validation
var zipval=document.regform.zip.value;

//Check null or not
if(zipval=="" || zipval==null)
{
alert("Zip code should not be blank");
document.regform.zip.focus();
return false;
}

if(isNaN(zipval) || zipval.length!=6)
{
alert("Zip code should not be contain alphabetic values and length should be 6");
document.regform.zip.focus();
return false;
}
//Interest checkbox validation
var chk_flag=false;

for(var i=0;i<document.regform.intr.length;i++)
{
if(document.regform.intr[i].checked)
{
chk_flag=true;
}
}

if(!chk_flag)
{
alert("Pleas select at least one interst");
return false;
}
//Gender radio button validation
var radio_flag=false;

for(var i=0;i<document.regform.gender.length;i++)
{
if(document.regform.gender[i].checked)
{
radio_flag=true;
}
}

if(!radio_flag)
{
alert("Pleas select gender");
return false;
}
//File validation
var file=document.regform.pic.value;

//Check null or not
if(file=="" || file==null)
{
alert("Please upload picture");
document.regform.pic.focus();
return false;
}

var ext=file.substring(file.lastIndexOf(".")+1);
if(ext!="jpg" && ext!="png" && ext!="gif")
{
alert("Please upload only jpg,png and gif images");
document.regform.pic.focus();
return false;
}
}
</script>
</head>
<body>
<fieldset>
<legend><strong>Member Registration</strong></legend>
<form name="regform" action="insert.html"onclick="validation()" method="post">
<table width="80%" align="center">
<tr>
<td>User Name:</td>
<td><input type="text" name="username" size="28" value="" maxlength="15"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="userpass" size="28" value="" maxlength="15"></td>
</tr>
<tr>
   <td>cpassword:</td>
<td><input type="password"name="cpass" size="28" value="" maxlength="15"></td>
</tr>
<tr>
<td>address:</td>
<td><textarea name="address" rows="10" cols="40"></textarea></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" size="28"/></td>
</tr>
<tr>
<td>States:</td>
<td>
<select name="state" >
<option value="">Select State</option>
<option value="AP" selected="selected">Andhra pradesh</option>
<option value="TN">Tamil Nadu</option>
<option value="Karnataka">Karnataka</option>
</select>
</td>
</tr>
<tr>
<td>Zip Code:</td>
<td><input type="text" name="zip" size="28"/></td>
</tr>
<tr>
<td>Interests:</td>
<td>
<input type="checkbox" name="intr" checked="checked" value="movies" />Movies
<input type="checkbox" name="intr" checked="checked"  value="sports" />Sports
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type="radio" name="gender" checked="checked" value="male" />Male
<input type="radio" name="gender" checked="checked"  value="female" />Female
</td>
</tr>
<tr>
<td>Upload Picture:</td>
<td><input type="file" name="pic" size="28"/></td>
</tr>
</table>
</form>
</fieldset>
</body>
</html>

No comments :

Post a Comment