feedburner

Enter your email address:

checking form validation using Javascript

Labels:

I've created a post of how to validates the form using Javascript.The form is shown below:

Form:

First Name
Last Name
Age
Email Address
Code:


<script language="javascript">

function validate()
{

email=document.forms[0].elements[3].value
if(document.f1.txtfname.value=="")
{
alert("First Name cannot be left empty ")
document.f1.txtfname.focus()
}
else if(document.f1.txtlname.value=="")
{
alert("Last name cannot be left empty ")
document.f1.txtlname.focus()
}
else if(document.f1.txtage.value=="")
{
alert("Age field cannot be left empty ")
document.f1.txtage.focus()
}
else if(email=="")
{
alert("Email field cannot be left empty ")
document.f1.txtemail.focus()
}

else if(isNaN(document.f1.txtage.value)==true)
{
alert("Age field should be Numeric ")
document.f1.txtage.select()
}
else if((document.f1.txtage.value)<=16)
{
alert("Age must be greater than 16 ")
document.f1.txtage.select()
}
else if((email.indexOf('@',1)==-1)(email.indexOf('.',1)==-1))
{
alert("You have entered Invalid email ")
document.f1.txtemail.select()
}
else
{
alert("Form is Ready to Submit ")
win=open("","","toolbar=no,status=no,titlebar=no,width=450 height=250")
win.document.write("<h2 align=center>Thankyou for submitting the information</h2>")
win.document.write("Your First Name is: "+document.f1.txtfname.value+"<p>")
win.document.write("Your Last Name is: "+document.f1.txtlname.value+"<p>")
win.document.write("Your Age is: "+document.f1.txtage.value+"<p>")
win.document.write("Your email address is: "+document.f1.txtemail.value+"<p>")
win.document.write("Total elements in the form: "+document.forms[0].length)
}
}

</script>

<table border=0 cellpadding=1 cellspacing=1 width=50>
<tr>
<form name=f1>
<td colspan=2><h2 align=center>Form Validation</h2>
<tr>
<td>First Name
<td><input type="text" name="txtfname">
<tr>
<td>Last Name
<td><input type="text" name="txtlname">
<tr>
<td>Age
<td><input type="text" name="txtage">
<tr>
<td>Email Address
<td><input type="text" name="txtemail">
<tr>
<td><input type="button" value="Submit" onclick="validate()"><input type="reset" value="Cancel">
</form>
</table>







0 comments:

Post a Comment

RSS FEED



Subscribe

Followers