PDA

View Full Version : JS Form validation



megha
11-19-2011, 12:26 AM
Hi
I want to create a javascript form validation but i cant validate with the following code.
What is wrong with the code? How to create a form validation without much modification of the following code-

<html>
<head><title>Javascript Validation</title>
<style>
.lf{
position:relative;
left: 30px;
float right;
}
</style>
<script>
function validate()
{
var c=document.getElementById("f1").value;
if(c.length<1)
alert(" Name Field can't be blank");
if(c.length<=5)
alert("Enter a valid name");
var a=parseInt(document.getElementById("f6").value);alert(a);
var b=document.getElementById("f7").value;alert(b);
var d=document.getElementById("f8").value;alert(d);
var lp=0;
if((d%400)==0 && (d%100==0) || (d%4==0))
lp=1;
if((a==31) && ((b=="4") || (b=="6") || (b=="9") || (b=="11")) )
alert("Date out of Range");
if((lp==0) && ((a=="29") || (b=="2") )
alert("Date out of Range");
}
</script>
</head>
<body>
<div style="width:50%;background-color:lightgray;">
<fieldset>
<legend style="color:brown;font-weight:bold;"> SignUp Form</legend>
<table height=300>
<form>
<tr><td> FirstName:&nbsp;&nbsp;<input type="text" name="fn" id="f1" class="lf" size=50 /></td></tr>
<tr><td> Address: &nbsp;&nbsp;&nbsp; <input type="text" name="adr" id="f2" class="lf" size=50 /></td></tr><br/><br/>
<tr><td> Zip:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="zp" id="f3" class="lf" size=20 /></td></tr><br/><br/>
<tr><td> UserName:&nbsp;<input type="text" name="fn" id="f4" class="lf" size=30 /></td></tr>
<tr><td> Password: &nbsp;&nbsp;<input type="password" name="fn" id="f5" class="lf" size=30 /></td></tr>
<tr><td> DOB: <br /><br />Day:&nbsp;&nbsp;&nbsp;<input type="text" name="dy" id="f6" />
Month:&nbsp;<input type="text" name="m1" id="f7" />
Year:&nbsp;<input type="text" name="m2" id="f8" /></td></tr>
<tr><td> Email:&nbsp;&nbsp;
<input type="text" name="fn" id="f9" size=50 /></td></tr>
<tr><td><center>
<input type="submit" value="submit" onClick="validate()" />
</center></td></tr>
</form>
</table>
</fieldset></div></body>
</html>