ASP. NET validation Controls

Source: Internet
Author: User

Asp. NET validation controls

Now that you can easily validate user input, you can also choose whether to validate on the server or on the client side, and you don't have to think about it so much that programmers can focus on the design of the main program.

Asp. NET public six kinds of validation controls, respectively, as follows:

Name of the control function describing
RequiredFieldValidator (must field validation) to check for input values
CompareValidator (compare validation) compare two inputs by setting
RangeValidator (range validation) whether the input is in the specified range
RegularExpressionValidator (regular expression validation) regular expression validation control
CustomValidator (custom validation) custom validation controls
ValidationSummary (verification Summary) Summary verification results

Below, let's look at the use of these controls:

I. Use of RequiredFieldValidator (mandatory field validation)

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

Controltovalidate= "Name of the control to check"
Errormessage= "error Message"
Display= "static| dymatic| None "
>
Placeholder

In the standard code above:
ControlToValidate: Indicates the control ID to be checked;
ErrorMessage: Indicates the error message that occurs when the 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 means that the page control is not occupied when the control error message appears, and none indicates that the error appears, but can be displayed in validatorsummary;
PLACEHOLDER: Indicates that when display is static, the error message occupies a "placeholder" so large page space;

Now, let's look at an example:

Controltovalidate= "Txtname"
Errormessage= "Name must be entered"
Display= "Static" >
* Name must be entered

In the example above, check if the Txtname control has input, and if not, display the error message "Name must be entered". Isn't it simple?
Note: The code above and the other controls below are best placed in the form, unlike in ASP, where the form is best written like this:


Other code

Thus, the form is executed on the server side, and the submission will be valid;

Second, CompareValidator (compare validation) control

Compare controls to compare the input of two controls to conform to the program settings, we do not compare only as "equal", although the equivalent is the most used, in fact, the comparison here includes a wide range, we see the standard code will understand.

The standard code for comparison controls is as follows:
Controltovalidate= "Control ID to validate"
Errormessage= "error Message"
Controltocompare= "Control ID to compare"
Type= "String| integer| double| datetime| Currency "
Operator= "equal| notequal| greaterthan| greatertanequal| lessthan| lessthanequal| DataTypeCheck "
Display= "static| dymatic| None "
>
Placeholder

In the standard code above:
Type represents the data type of the control to compare;
Operator represents the comparison operation (that is, why the comparison is not just "equal"), here, there are 7 ways to compare;
Other attributes are the same as RequiredFieldValidator;
Here, pay attention to the difference between ControlToValidate and ControlToCompare, if operate for Greatethan, then, Must controltocompare greater than ControlToValidate is legal, this, should understand their meaning of the two? For example, refer to the RequiredFieldValidator control, which is designed against standard code.

Iii. RangeValidator (range validation) controls

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

Controltovalidate= "Control ID to validate"
Type= "Integer"
Minimumvalue= "Minimum Value"
Maximumvalue= "Maximum Value"
Errormessage= "error Message"
Display= "static| dymatic| None "
>
Placeholder

In the above code:
Use the MinimumValue and MaximumValue to define the value of the control input, and type to determine the types of the control's input values.

Iv. Regularexpresionvalidator (regular expression) control

Regular expression validation controls are very powerful, and you can easily construct your own validation methods, so let's look at the standard code first:

Controltovalidate= "To verify the control name"
Validationexpression= "Regular expression"
Errormessage= "error Message"
Display= "Static"
>
Placeholder

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 "*" is expressed in conjunction with other expressions and is easy to assemble;
"[A-z]" denotes any uppercase letter;
"\d" means a number easily;
Note that in the above expression, the quotation marks are not included;
Example:
Regular expression: ". *[a-z]" indicates that any combination of characters beginning with a number is 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:

headertext= "header Information"
Showsummary= "true| False "
Diaplaymode= "list| Bulletlist| Singleparagraph "
>

In the standard code above, Headtext corresponds to the headtext,displaymode of the table to indicate how the error message is displayed: List is equivalent to
in HTML, BulletList is equivalent to

  • in HTML. ; Singleparegraph indicates that there is no separation between error messages;

  • Vi. CustomValidator (custom validation) control

    The control defines the authentication method with a custom function with the following standard code:

    Controltovalidate= "Controls to validate"
    onservervalidatefunction= "Validation Function"
    Errormessage= "error Message"
    Display= "static| dymatic| None "
    >
    Placeholder

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

    Vii. Summary
    Asp. NET validation control has been introduced, you can see that they are very powerful, and do not worry about verification. Of course, the above introduction is not particularly detailed, subtle places, but also please read the Microsoft SDK.

    ASP. NET validation Controls

    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.