Jquery validation: making verification so easy

Source: Internet
Author: User

As we all know, jquery makes countless people crazy with its simplicity. Now I want to introduce a jquery validation as you can see. The first intuitive feeling is that this is definitely a verification framework, and there is no error. This article is based on jquery validation. Source code download

Client verification can effectively reduce the number of data transfers between the server and the client, improve the resource utilization path of the server, and provide users with intuitive and fast responses. In the Web 2.0 era, this is especially important. I think everyone must hate using basic JavaScript to write annoying verification. Now there is a better framework for server-side verification, Asp. net MVC can do this well, so. for net development, having a good client rectification framework plays a vital role in improving development efficiency.

We can get this JS framework through the address below

    • Jquery
    • Jquery validation framework

Let's start with a simple example.Jquery validation frameworkYes

First, we need to add references to the above two JS files.

< Head runat = " Server " >
< Title > Untitled page </ Title >
< Script SRC = " Jquery. js " Type = " Text/JavaScript " > </ Script >
< Script SRC = " Jquery. Validate. js " Type = " Text/JavaScript " > </ Script >
</ Head >

 

Next, declare the following HTML section

 

< Form ID = " Customerform " Runat = " Server " >
< Div >

First name: < Input type = " Text " ID = " Firstname "   Class = " Required " Name = " Firstname "   />

Last name: < Input type = " Text " ID = " Lastname "   Class = " Required " Name = " Lastname "   />  


< Input type = " Submit " Value = " Register "   />

</ Div >
</ Form >

Pass the aboveCode, You will find that we add class = "required" to every input, which means an error will be prompted when the inpute label is empty.

Finally, we need to find a starting point for our framework. Generally, we can put the following code at the end of HTML.

< Script Language = " Javascript " Type = " Text/JavaScript " >

$ (Document). Ready (function ()
{
$ ("# Customerform"). Validate ();
});

</Script>

Run the command to see how it works

 

 

 

The following is an example. ListBox controlCreate regular verification

We can add rules in this way.

$ ( " # Customerform " ). Validate (
{
Rules:
{
Firstname: {required: True },
Lastname: {required: True },
Countries: {validatecountries: True }
},

Messages:
{
Firstname: {required: " First name is required " },
Lastname: {required: " Last name is required " },
Countries: {validatecountries: " Please select at least 2 items from the countries " }
},


});
  // Add the validate countries Method
Jquery. validator. addmethod ( " Validatecountries " , Function (value, element)
{

VaR noofselectedcountries = $ ( " # Countries: Selected " ). Length;
If (Noofselectedcountries <   2 ) Return   False ;

Return   True ;
});

 

 

Provide Error information for errors

$ ( " # Customerform " ). Validate (
{
Rules:
{
Firstname: {required: True },
Lastname: {required: True },
Countries: {validatecountries: True }
},

Messages:
{
Firstname: {required: " First name is required " },
Lastname: {required: " Last name is required " },
Countries: {validatecountries: " Please select at least 2 items from the countries " }
},

Errorcontainer: " # Errors " ,
Errorlabelcontainer: " # Errors ul " ,
Wrapper: " Li "

});


 

For more information, see

 

 

 

Okay, I will not mention the content provided in this Article.Source codeIt's very late to download your own research.

Source code download

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.