Validation with jQuery
How to validate a form using jquery
Description
The code demonstrate how to validate a form using jquery.Let's take an example of registration page.There are several form that user have to fill up for registration.So you need to make mandatory some fields also.If you want make some fields as mandatory then look the below code.
Put all the codes inside
$("#aspnetForm").validate({
//your validation
}
$("#aspnetForm").validate({
rules:
{
<%=txtFirstName.UniqueID%>:{required:true},
<%=txtLastName.UniqueID%>:{required:true},
<%=txtAddress1.UniqueID%>:{required:true},
<%=txtCity.UniqueID%>:{required:true},
<%=txtState.UniqueID%>:{required:true},
<%=txtPostCode.UniqueID%>:{required:true},
}
If you want to make specify minimum length the textbox
<%=txtPassword.UniqueID%>:
{
required: true,
minlength: 5
},
You want to compare a field with another field then use like below.Here confirm textbox must have value as pssword textbox
<%=txtConfirmPassword.UniqueID%>:
{
required: true,
minlength: 5,
equalTo:'#<%=txtPassword.ClientID%>'
},
If you want to show message to user for validation then use like below code
<%=txtFirstName.UniqueID%>:"Enter First Name.",
<%=txtLastName.UniqueID%>:"Enter Your Last Name.",
For entering only number
<%=txtContactNo.UniqueID%>:
{
required:"Please Enter Contact No",
number: "Enter A valid Number"
},
Foe showing message for minimum number of character
<%=txtConfirmPassword.UniqueID%>:
{
required: "Repeat your password",
minlength: jQuery.format("Enter at least {0} characters"),
equalTo: "Enter the same password as above"
},
To show the message that user have to enter same entry as another field like i confim password then
<%=txtConfirmPassword.UniqueID%>:
{
required: "Repeat your password",
minlength: jQuery.format("Enter at least {0} characters"),
equalTo: "Enter the same password as above"
},
Thanks
hi