Use jquery. Validate. js under the aspx multi-server control

Source: Internet
Author: User

Jquery. Validate. JS is a front-end verification control based on the jquery library. It has powerful functions and integrates many common verification methods. Very convenient. As follows:

// Verify
$ ( Function (){
// After the document is loaded, bind the validation rules to the form. In this way, the verification is automatically performed when the form is submit.
$ ( " # Aspnetform " ). Validate ({
Rules :{

Txtage :{
Required: True ,
Number: True ,
Min: 18
},
},
Messages :{
Txtage :{
Required: " Required " ,
Number: " Enter a number " ,
Min: jquery. Format ( " Must be at least {0 }. " )
},
}

});
});

The operating principle is to bind some verification methods to the form in HTML. It is automatically verified when the form is submitted and blocked when the verification fails. This design concept is correct and feasible. It can be understood why the designer did not bind the event to a button. It intercepts key points. This effectively prevents multiple submit instances. There is a fish that has missed the internet!

However, ASP. NET developers have a headache. The reason is that the buttons dragged from the control panel are converted to submit on the page. In most cases, the business rules of each submit are different. Therefore, jquery. Validate is especially required to be able to verify and bind with a specific submit as the target. Unfortunately, I did not find any direct support for the official API today!

But still find useful value! Jquery. Validate provides methods to delete specific verification. This is easy to handle. We use the target button to bind the form for verification, and then bind a method to remove the verification from the irrelevant submit. RelatedCodeAs follows:

<% @ Page Language = " C # " Autoeventwireup = " True " Codebehind = " Decompress. aspx. CS " Inherits = " Qsqmanage. Decompress "   %>

<!Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

< Html Xmlns = "Http://www.w3.org/1999/xhtml"   >
< Head Runat = "Server" >
< Title > Demo </ Title >

< Script Language = "JavaScript" SRC = "/Jslib/jquery-1.3.2.min.js" Type = "Text/JavaScript" > </ Script >
< Script Language = "JavaScript" SRC = "/Jslib/jplug/jquery. Validate. Min. js" Type = "Text/JavaScript" > </ Script >  
< Script Type = "Text/JavaScript" >
// Binding Form Verification
Function Needvalidate ()
{
$ ( " # Aspnetform " ). Validate ({
Rules :{
Txtage :{
Required: True ,
Number: True ,
Min: 18
}
},
Messages :{
Txtage :{
Required: " Required " ,
Number: " Enter a number " ,
Min: jquery. Format ( " Must be at least {0 }. " )
}
}
});
}
// Cancel Form Verification
Function Novalidate ()
{
$ ( " # Txtage " ). Rules ( " Remove " ); // If you cancel multiple
}

$ ( Function (){
// Bind a verification form to the control to be verified
$ ( " # Button1 " ). Click ( Function (){
Needvalidate ();
});

// Unauthenticated binding of controls that do not require verification
$ ( " # Button2 " ). Click ( Function (){
Novalidate ();
});
});
</ Script >
</ Head >
< Body >
< Form ID = "Aspnetform" Runat = "Server" >
< Div >

Age: < ASP: textbox ID = "Txtage" Runat = "Server" > </ ASP: textbox >

< ASP: button ID = "Button1" Runat = "Server" Onclick = "Button#click" Text = "Verification required"   />
< ASP: button ID = "Button2" Runat = "Server" Onclick = "Button2_click" Text = "Verification not required"   />

</ Div >

</ Form >
</ Body >
</ Html >

The background is very simple,

Protected   Void Button#click ( Object Sender, eventargs E)
{
Response. Write ( " Verification passed " );
}

Protected VoidButton2_click (ObjectSender, eventargs E)
{
Response. Write ("Not verified");
}

In this way, click button1 to verify, while button2 directly skips.

If a page has many other buttons, you can use jquery's powerful selector to cancel verification in batches. As follows:

$ (Document). Ready ( Function (){
// Bind a verification form to the control to be verified
$ ( " # Button1 " ). Click ( Function (){
Needvalidate ();
});

// Unauthenticated binding of controls that do not require verification
// $ ("# Button2"). Click (function (){
// Novalidate ();
// });

// Batch unbind verification: All types = submit and names are not button1.
$ ( " Input [type = 'submit '] [name! = 'Button1'] " ). Click ( Function (){
Novalidate ();
});
});

 

Chu Xuan blog Park

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.