Asp. NET common 6 kinds of validation control introduction

Source: Internet
Author: User
Tags comparison range regular expression uppercase letter valid

  RequiredFieldValidator (mandatory field validation) is used to check for input values

CompareValidator (comparison validation) compares two inputs by setting

RangeValidator (range validation) Enter whether the specified range

RegularExpressionValidator (regular expression validation) regular expression validation control

CustomValidator (custom validation) custom validation controls

ValidationSummary (validation Summary) Summary validation Results

  Here are some ways to use these controls

  CompareValidator (comparison validation) control 

Compare controls to compare the input of two controls to match the program settings, we do not just understand the comparison is "equal", although the same is the most used, in fact, the comparison here covers a wide range, we see the standard code will understand.

The standard code for comparing controls is as follows:

<asp:comparevalidator id= "validator_id" runat= "Server"

Controltovalidate= "The control ID to validate"

Errormessage= "error Message"

Controltocompare= "The control ID to compare"

Type= "String| integer| double| datetime| Currency "

Operator= "equal| notequal| greaterthan| greatertanequal| lessthan| lessthanequal| DataTypeCheck "display=" static| dymatic| None "

> Placeholder </ASP:CompareValidator>

In the above standard code:

Type represents the data type of the control to be compared;

Operator represents comparison operations (that is, why the comparison is not just "equal" reason), here, compared to 7 ways;

Other attributes are the same as RequiredFieldValidator;

Here, pay attention to the difference between ControlToValidate and ControlToCompare, if operate is Greatethan, then you must controltocompare greater than controltovalidate Is legal, should you understand the meaning of both of them? Example program please refer to the RequiredFieldValidator control and design it against the standard code.

  Second, the use of RequiredFieldValidator (must field validation)

  The standard code used by the RequiredFieldValidator control is as follows:

<asp:requiredfieldvalidator id= "Validator_name" runat= "controltovalidate=" Control name "errormessage=" error message to check Display= "static| dymatic| None ">

Placeholder

</asp:requiredfieldvalidator >

In the above standard code:

ControlToValidate: Indicates that the control ID is to be checked;

ErrorMessage: Indicates an error message that occurs when a check is not valid;

Display: The way the error message is displayed; Static indicates that the error message of the control occupies a positive position on the page; dymatic indicates that the control error message takes up the page control when it appears; none is displayed when the error occurs, but it can be displayed in the validatorsummary;

PLACEHOLDER: When display is static, the error message occupies a page space of "placeholder" so large;

Now, let's look at an example:

<asp:textbox id= "txtname" runat= "Server"/>

<asp:requiredfieldvalidator id= "Validator1" runat= "controltovalidate=" txtname "errormessage=" name must be entered " Display= "Static" & gt;

* Name must be entered

</ASP:RequiredFieldValidator>

In the above example, check that the Txtname control has input, and if not, display the error message "name must be entered." Is it simple?

Note: The code for the above code and the other controls below is best placed in the form, and unlike the ASP, the form is best written like this:

<form runat= "Server" >

Other code

</Form> Thus, the Form is executed on the server side, and the commit is valid;

  Iii. RangeValidator (scope validation) control 

Verify that the input is in a certain range, the range is determined with MaximumValue (max) and Minimunvlaue, and the standard code is as follows:

<asp:rangevalidator id= "vaidator_id" runat= "Server" controltovalidate= "the control ID" type= "Integer" minimumvalue= "to be validated Min value "maximumvalue=" Maximum "errormessage=" error message "display=" static| dymatic| None ">

Placeholder

</ASP:RangeValidator>

In the above code:

Use MinimumValue and MaximumValue to define the range of control input, and type to define the types of control input values.

  Four, regularexpresionvalidator (regular expression) control 

The regular expression validation control is very powerful, you can easily construct the verification method, we first look at the standard code:

<asp:regularexpressionvalidator id= "validator_id" runat= "Server" controltovalidate= "to validate control name" Validationexpression= "Regular expression" errormessage= "error message" display= "Static" >

Placeholder

</ASP:RegularExpressionValidator>

In the above standard code, ValidationExpression is the focus, now look at its construction:

In ValidationExpression, different characters represent different meanings:

"." Denotes any character;

The expression "*", together with other expressions, represents an easy combination;

"[A-z]" denotes any uppercase letter;

"D" means an easy number;

Note that in the above expression, the quotation marks are not included;

Example:

Regular expression: ". *[a-z]" denotes any combination of characters at the beginning of the number followed by an uppercase letter.

  V. ValidationSummary (validation Summary) control 

The control collects all validation error messages on this page and can organize them to be displayed later. The standard code is as follows:

<asp:validationsummary id= "validator_id" runat= "Server" headertext= "header information" showsummary= "true| False "Diaplaymode=" list| Bulletlist| Singleparagraph ">

</asp:validationsummary >

In the above standard code, the Headtext corresponds to the headtext of the table, DisplayMode indicates how the error message is displayed: The List corresponds to the <BR> bulletlist in HTML, which corresponds to <LI> in HTML. Singleparegraph indicates that there is no segmentation between error messages;

  Vi. CustomValidator (custom validation) controls 

The control uses a custom function to define the authentication method, and its standard code is as follows:

<asp:customvalidator id= the control "controltovalidate=" validation function to be validated by the "validator_id" runat= "Server onservervalidatefunction=" "Errormessage=" error message "display=" static| dymatic| None > Placeholder </asp:customvalidator >

In the above code, the user must define a function to validate the input

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.