Verify the use of controls in ASP. NET

Source: Internet
Author: User

Preface:

A few days ago, I had no choice but to use JavaScript to judge the effectiveness of the control. It was indeed a laborious and laborious task! Especially for the JS verification of regular expressions such as the mail format and postal code (which involves the comparison of regular expressions, etc., it is annoying ~). In fact, for the validity verification of these commonly used controls, there is a separate Validation Control in ASP. NET for use. They can meet General validation requirements, such as non-null, range, and comparison.

 

Verification control:

The Built-in validation controls in ASP. NET include:Requiredfieldvalidation, rangevalidation, regularexpressvalidation, comparevalidation, customvalidation, and validationsummary. The user-defined verification control is not discussed here because it is not very common (many components, including functions, need to be customized.

The simple verification interface is as follows (design view ):

 

 

 

The error message of the verification control is displayed in red on the right of the selected environment. DesignedEnvironment LayoutAs follows:

1) The controltovalidate attribute of the requiredfieldvalidation control is the text box corresponding to the name. For the controltovalidate attribute, each verification control corresponds to one and serves as the verification subject;

2) rangevalidation control, whose controltovalidate attribute is the age text box;

3) comparevalidation control, whichControltovalidateThe attribute is the text box for Password confirmation. There is also an attribute that is easy to confuse with the controltovalidate attribute:ControltocompareAttribute, the text box corresponding to the password.Differentiation:The main point is to distinguish the subject. At this time, the verification subject is the Password Confirmation corresponding box. When operator is used (equal to, greater than, less than), it is the comparison subject, compared with the compared text box, here is the text box corresponding to the password.

4) The regularexpressvalidation control, whose controltovalidate attribute is the text box corresponding to the mailbox;

5) validationsummary control, which does not have the controltovalidate attribute;

 

Its corresponding front-endCodeIs:

< Head Runat = "Server" >
< Title > Verification control demo page </ Title >
< Script Type = "Text/JavaScript"   >
// Description: demonstrates the use of verification controls one by one as a review.

// Copyright: http://www.cnblogs.com/yangmingming

// Notes: An Example of simulating a user table is used for verification.
</ Script >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
Name: < ASP: textbox ID = "Txtrequiredfield" Runat = "Server" > </ ASP: textbox >
< ASP: requiredfieldvalidator ID = "Requiredfieldvalidator1" Runat = "Server"  
Errormessage = "Name cannot be blank! " Controltovalidate = "Txtrequiredfield" > </ ASP: requiredfieldvalidator >
< BR />
Age: < ASP: textbox ID = "Txtrange" Runat = "Server" > </ ASP: textbox >
< ASP: rangevalidator ID = "Rangevalidator1" Runat = "Server"  
Errormessage = "Age is not within the prescribed range! " Controltovalidate = "Txtrange" Maximumvalue = "100"  
Minimumvalue = "0" Type = "Integer" > </ ASP: rangevalidator >
< BR />
Password: < ASP: textbox ID = "Txtpassword" Runat = "Server" > </ ASP: textbox >
< BR />
Password confirmation: < ASP: textbox ID = "Txtpasswordconfirm" Runat = "Server" > </ ASP: textbox >
< ASP: comparevalidator ID = "Comparevalidator1" Runat = "Server"  
Errormessage = "Password entered before and after, inconsistent! " Controltocompare = "Txtpassword"  
Controltovalidate = "Txtpasswordconfirm" > </ ASP: comparevalidator >
< BR />
Email: < ASP: textbox ID = "Txtmail" Runat = "Server" > </ ASP: textbox >
< ASP: regularexpressionvalidator ID = "Regularexpressionvalidator1" Runat = "Server"  
Errormessage = "Invalid email format! " Controltovalidate = "Txtmail"  
Validationexpression = "\ W + ([-+. '] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W + )*" > </ ASP: regularexpressionvalidator >
< BR />
< ASP: validationsummary ID = "Validationsummary1" Runat = "Server"  
Headertext = "Verification information Summary"   />
< BR />
< ASP: button ID = "Btnsubmit" Runat = "Server" Text = "Submit" Onclick = "Btnsubmit_click"   />  
</ Div >
</ Form >
</ Body >
</ Html >

When the input in the text box does not match, the following page appears:

 

Appendix:

For all the requiredfieldvalidation controls, when they are only for the Name box, when other boxes are empty, their corresponding verification controls areInvalid.It can be considered that except the requiredfieldvalidation control, other controls can only function on the basis of this control.

 

 

In summary, the use of verification controls is combined to demonstrate the use of each verification control and to review the use of verification controls in the future ~

 

 

 

 

 

 

Related Article

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.