Original: ASP. NET validation of the use of controls
Objective:
A few days ago, helpless to use JS to judge the effectiveness of the control, it is really a laborious, difficult thing to find! Especially for the message format, postal code, and so on the regular expression of the JS verification (which involves the comparison of regular expressions, more annoying ~). In fact, for these commonly used control validation, there are separate validation controls available in ASP. They can satisfy general, such as non-empty, range, comparison and so on validation.
Validation controls:
The validation controls built into asp:requiredfieldvalidation, Rangevalidation, Regularexpressvalidation, Comparevalidation, CustomValidation and ValidationSummary , etc. six kinds. The user-defined validation controls, which are not very commonly used (to customize many components, including functions, etc.), are not discussed in this article.
The simple verification interface is as follows (Design view):
Shows the selection environment, the right red font is the errormessage information for the validation control. The design environment is arranged as follows:
1) The RequiredFieldValidation control whose ControlToValidate property is the name corresponding to the text box. For the ControlToValidate property, each validation control corresponds to one, as the body of the validation;
2) Rangevalidation control, whose ControlToValidate property is the age corresponding text box;
3) The Comparevalidation control, whose ControlToValidate property is the text box for the password confirmation. There is also an attribute that is easily confused with the ControlToValidate property: TheControlToCompare property, which corresponds to a text box for the password. distinguish: The main point is to distinguish the subject, at this time the main body of the verification is the password confirmation box, when using operator (equals, greater than, less than), is the main body of comparison, and the comparison of the text box, here is the password corresponding text box.
4) Regularexpressvalidation control, whose ControlToValidate property is the text box corresponding to the mailbox;
5) ValidationSummary control, it does not have the ControlToValidate property;
The corresponding foreground code is:
<Headrunat= "Server">
<title>validation control's demo page</title>
<Scripttype= "Text/javascript" >
//Description: By demonstrating the use of validation controls individually, as a review
//copyright:http://www.cnblogs.com/yangmingming
//Notes: Using an example of a mock user table to verify
</Script>
</Head>
<Body>
<formID= "Form1"runat= "Server">
<Div>
Name:<Asp:textboxID= "Txtrequiredfield"runat= "Server"></Asp:textbox>
<Asp:requiredfieldvalidatorID= "RequiredFieldValidator1"runat= "Server"
ErrorMessage= "Name cannot be empty!" "ControlToValidate= "Txtrequiredfield"></Asp:requiredfieldvalidator>
<BR/>
Age:<Asp:textboxID= "Txtrange"runat= "Server"></Asp:textbox>
<Asp:rangevalidatorID= "RangeValidator1"runat= "Server"
ErrorMessage= "Age is not within the prescribed range!" "ControlToValidate= "Txtrange"MaximumValue= "+"
MinimumValue= "0"Type= "Integer"></Asp:rangevalidator>
<BR/>
Password:<Asp:textboxID= "Txtpassword"runat= "Server"></Asp:textbox>
<BR/>
Password Confirmation:<Asp:textboxID= "Txtpasswordconfirm"runat= "Server"></Asp:textbox>
<Asp:comparevalidatorID= "CompareValidator1"runat= "Server"
ErrorMessage= "Password before and after input, inconsistent!" "ControlToCompare= "Txtpassword"
ControlToValidate= "Txtpasswordconfirm"></Asp:comparevalidator>
<BR/>
Email:<Asp:textboxID= "Txtmail"runat= "Server"></Asp:textbox>
<Asp:regularexpressionvalidatorID= "RegularExpressionValidator1"runat= "Server"
ErrorMessage= "Mailbox format does not match!" "ControlToValidate= "Txtmail"
ValidationExpression= "\w+ ([-+. '] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * "></Asp:regularexpressionvalidator>
<BR/>
<asp:validationsummaryID= "ValidationSummary1"runat= "Server"
HeaderText= "Summary of verification information" />
<BR/>
<Asp:buttonID= "Btnsubmit"runat= "Server"Text= "Submit"onclick= "btnSubmit_Click" />
</Div>
</form>
</Body>
</HTML>
When the text box input does not match, the interface appears as:
Report:
For all requiredfieldvalidation controls, when they are only for the name box, their corresponding validation controls are invalidated when the rest of the boxes are empty . you can assume that except for the RequiredFieldValidation control, the rest of the controls must be based on this control to work.
Summary, the use of the validation control to synthesize, to demonstrate the use of various validation controls, for future validation control use to do a review, hehe ~