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


Comments

Author: Abhay Kumar Raut22 Feb 2010 Member Level: Gold   Points : 1

hi

Author: Jayendra Kumar22 Feb 2010 Member Level: Gold   Points : 0

i have to write this code in jquery file. Am i right?

Author: Abhay Kumar Raut22 Feb 2010 Member Level: Gold   Points : 1

You need add this code in the client side .But you neeed to first download the jquery and use it in your project.I am going to attach sample file.

Author: Abhay Kumar Raut23 Feb 2010 Member Level: Gold   Points : 1

Firsttime the resource had 5 points then I submitted again then i got 8 point.Then again someone reduced point to 5 point.Then again point reduced to 2.Can someone explain this?

Author: ABitSmart24 Feb 2010 Member Level: Gold   Points : 0

I have assigned 10 points to it now.

Author: Jayendra Kumar24 Feb 2010 Member Level: Gold   Points : 0

How the points be increment? can any one tell me?

Author: Abhay Kumar Raut24 Feb 2010 Member Level: Gold   Points : 1

That depends upon quality of resource and to be edited by the editors or webmaster.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: