Use of the "reprint" C # Validation control using the method (RegularExpressionValidator)

Source: Internet
Author: User
Tags uppercase letter

Controltovalidate= "Name of the control to validate"
validationexpression= "Validation Rules"
Errormessage= "error message to display"

The validation features and their validation expressions when using RegularExpressionValidator validation controls are described below:

Only numbers can be entered: "^[0-9]*$"
Only n digits can be entered: "^\d{n}$"
You can only enter at least n digits: "^\d{n,}$"
Only m-n digits can be entered: "^\d{m,n}$"
Only numbers starting with 0 and non-0 can be entered: "^ (0|[ 1-9][0-9]*) $ "
You can only enter a positive real number with two decimal places: "^[0-9]+ (. [ 0-9]{2})? $ "
You can only enter a positive real number with 1-3 decimal places: "^[0-9]+ (. [ 0-9]{1,3})? $ "
You can only enter a non-zero positive integer: "^\+?" [1-9] [0-9]*$]
Only non-zero negative integers can be entered: "^\-[1-9][0-9]*$"
Only characters with a length of 3 can be entered: "^. {3}$ "
You can only enter a string consisting of 26 English letters: "^[a-za-z]+$"
You can only enter a string consisting of 26 uppercase English letters: "^[a-z]+$"
You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$"
You can only enter a string consisting of a number and 26 English letters: "^[a-za-z0-9]+$"
You can only enter a string consisting of a number, 26 letters, or underscores: "^\w+$"
Verify user password: "^[a-za-z]\w{5,17}$" is in the correct format: start with a letter, length between 6-18, and can contain only characters, numbers, and underscores.
Verify that it contains ^%& ',; =?$ "characters:" [^%& ',; =?$\x22]+ "
Only Chinese characters can be entered: "^[\u4e00-\u9fa5],{0,}$"
Verify email Address:"\w+ ([-+. '] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * "

Verify InternetURL: "^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)? $ "
Verify the phone number: "^ (\ (\d{3,4}\) |\d{3,4}-)? \d{7,8}$"

The correct format is: "Xxxx-xxxxxxx", "xxxx-xxxxxxxx", "xxx-xxxxxxx", "xxx-xxxxxxxx", "XXXXXXX", "XXXXXXXX".

Verify your Social Security number (15-bit or 18-digit number): "\d{17}[\d| X]|\D{15} "
Validation 12 months of the year: "^ (0?[ 1-9]|1[0-2]) $ "correct format:" 01 "-" 09 "and" 1 "" 12 "
Verify one months of 31 days: "^ ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ "

The correct format is: "01" "09" and "1" "31"

ValidationExpressionvalidation rules
Where the validationexpression validation rule attribute is a description of the restriction data input, its common symbols are shown in the following table:

[] symbols
The "[]" symbol can be used to define an accepted single character, for example:
[A-za-z] accepts only A-Z or a-z English characters.
[X-zx-z] receives only lowercase x-z or uppercase x-z.
[win] receives only the English letters of W, I, N.
[^linux] In addition to L, I, N, u, x, the English alphabet are received.
{} symbols
The "{}" symbol can be used to indicate how many characters are received, such as:
[A-za-z] {4} Indicates acceptance of only four characters.
[A-z] {4} means only a total of four A-Z lowercase characters are received.
[A-za-z] {4,6} indicates a minimum of four characters and accepts up to six characters.
[A-za-z] {4,} indicates a minimum of four characters, up to No limit.
.symbols
「.」 Symbols can be used to indicate that any character except whitespace is received, for example:
. {4} indicates that four characters are received, except for whitespace.
*symbols
The "*" symbol represents a minimum of 0 matches, up to an infinite number of characters. For example:
[a-za-z]* indicates unlimited number, accepts A-Z or a-Z character, or does not enter.]
+symbols
The "+" symbol represents at least one match, up to an infinite number of characters. For example:
[a-za-z]+ indicates unlimited number, accepts A-Z or a-Z character, but enters at least one character.]
The following example restricts the account entered by the user, must start with an English letter, and enter a minimum of four characters and can enter up to eight characters:

===========================================================

How to use C # validation controls
Asp. NET provides developers with a complete set of server controls to verify that the information entered by the user is valid. These controls are as follows:
1, RequiredFieldValidator: Verify a required field, if this field is not filled, then, you will not be able to submit information.

2, CompareValidator: Comparative verification. Compares two field values for equality, such as password and Confirm password two fields are equal; Compares a field to a specific value.

3, RangeValidator: scope verification. Verify that a field is in a range, such as the score field if it is in the 0~100 range.

4, RegularExpressionValidator: Regular expression validation. It validates the format of the user input field according to the regular expression, such as e-mail, ID, phone number, and so on.

5. CustomValidator: This control can be used when running custom client-side JavaScript or VBScript functions.

So, how do you use validation controls?

1. Use the visual Studio. NET 2003 command prompt tool to run the ASPNET_REGIIS-C command to copy the ASP.

2. Place the validation control on the right side of the control you want to validate.

3. Modify the ControlToValidate property of the validation control to the name of the control to validate.

4. Modify the ErrorMessage property of the validation control to more specific error description information, such as "Please enter the correct e-mail password", "Password Inconsistent", "required fields" and so on.

5, if the use of RequiredFieldValidator required validation control, to this step is OK. However, if you are using several other controls, you need to make the following settings:

A, CompareValidator control, compare the values of two controls, to set the ControlToCompare, operator, and type properties. If you are comparing a control with a specific value, you need to modify the ValueToCompare, operator, and type properties.

B, RangeValidator controls, set the minimum and maximum values in the Minimunvalue and MaximumValue properties, and change the Type property to Currency or integer, respectively.

C, regularexpress control: Select a new regular expression from the list by using the Validationexpress property.

Finally, it is important to note that each field can be validated with several poorly typed 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 indicating how the error message is displayed: list is equivalent to the 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.

    Use of the "reprint" C # Validation control using the method (RegularExpressionValidator)

    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.