Thursday, March 15, 2012

Validations in Registration Form

This javascript validates any registration form.
For eg:-
  • If a user enters numbers in the text fiel in front Name, then it's invalid value assignment for the attribute Name.
    Following is the javascript for the same:-
<html>
<head>
<script language="javascript" type="text/javascript">
function validate()
{
/*if((document.f1.t1.value=="")||(document.f1.t2.value=="")||(document.f1.t3.value=="")||(document.f1.t4.value=="")||(document.f1.t5.value==""))
{
    alert("please fill ALL Details");
}*/
if(document.f1.t3.value=="")
{
    document.f1.l3.value="enter password";
}
if(document.f1.t3.value!=document.f1.t4.value )
{
        document.f1.l3.value="invalid password";
}
var a=document.f1.t1.value;
document.f1.l1.value=testchar(a);
function testchar(m)
{
    var p="^[a-zA-Z]*$";
    var r=new RegExp(p);
    return r.test(m);
}
var b=document.f1.t2.value;
document.f1.l2.value=testnum(b);
function testnum(n)
{
    var s="^[0-9]*$";
    var v=new RegExp(s);
    return v.test(n);
}
var c=document.f1.t5.value;
document.f1.l5.value=testmail(c);
function testmail(l)
{
    var t="^[\\w-_\*]\*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var x=new RegExp(t);
    return x.test(l);
}

}
</script>
</head>
<body>
<form name="f1">

Name
<input type="text" name="t1" height="100" width="100"/>
<input type="label" name="l1" height="100" width="100 " />
<br />
Contact
<input type="text" name="t2" />
<input type="label" name="l2" />
<br />
Password
<input type="text" name="t3" />
<input type="label" name="l3" />
<br />
Confirm password
<input type="text" name="t4" />
<input type="label" name="l4" />
<br />
E-mail
<input type="text" name="t5" />
<input type="label" name="l5" />
<br />
<input type="button" name="b1" value="ok" onclick="validate();" />

</form>
</body>
</html>
 

No comments:

Post a Comment