Regularexpressionvalidator for ASP. NET verification controls

Source: Internet
Author: User

 

The regularexpressionvalidator control is used to verify whether the input value matches the pattern specified by the regular expression. For example, email, phone number, zip code, integer, and decimal number. Regular Expressions can be used to combine various modes. Almost everything you want can be written. Let's take a look at this magic control.

Regularexpressionvalidator attributes:

Attribute

Description

Backcolor

Background Color

Controltovalidate

ID of the verified Control

Display

Verify the display behavior of the control.

Valid values include:

· None-verification messages are never displayed inline.

· Static-allocate space in the page layout for displaying verification messages.

· Dynamic-If verification fails, the space used to display verification messages is dynamically added to the page.

Enableclientscript

Boolean value that specifies whether to enable client verification.

Enabled

Boolean value that specifies whether to enable the verification control.

Errormessage

Text displayed in the validationsummary control when verification fails.

Note: If the text property is not set, the text is displayed in the verification control.

Forecolor

The foreground color of the control.

ID

The unique ID of the control.

Isvalid

Boolean value indicating whether the associated input control has passed verification.

Runat

Specifies that the control is a server control. Must be set to "server ".

Text

Message displayed when verification fails.

Validationexpression

Verify the regular expression of the input control. On the client and server, the expression syntax is different.

 

Let's look at an example:

 <%  @ Page Language  = "  C #  "  Autoeventwireup  =  "  True  "  Codebehind  =  "  Enter your email address and phone number. aspx. CS "  Inherits  =  "  Webapplication1. enter your email address and phone number.  "   %>  <!  Doctype html  >  <  Html  Xmlns = "Http://www.w3.org/1999/xhtml"  >  <  Head  Runat  = "Server"  >  <  Meta  HTTP-equiv  = "Content-Type"  Content  = "Text/html; charsets = UTF-8"  />      <  Title > </  Title  >  </  Head  >  <  Body  >      <  Form  ID  = "Form1"  Runat  = "Server"  >              < P  >  Mail box:  <  ASP: textbox  ID  = "Txtmail"  Runat  = "Server"  > </  ASP: textbox  >              <  ASP: regularexpressionvalidator  ID  = "Regularexpressionvalidator1" Forecolor  = "Red"  Runat  = "Server"  Errormessage  = "Enter the correct email address"  Validationexpression  = "\ W + ([-+. '] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W + )*"  Controltovalidate  = "Txtmail"  > </  ASP: regularexpressionvalidator  >          </  P >          <  P  >  Mobile phone number:  <  ASP: textbox  ID  = "Txtphone"  Runat  = "Server"  > </  ASP: textbox  >              <  ASP: regularexpressionvalidator ID  = "Regularexpressionvalidator2"  Forecolor  = "Red"  Runat  = "Server"  Errormessage  = "Enter the correct mobile phone number"  Validationexpression  = "(86) * 0*13 \ D {9 }"  Controltovalidate  = "Txtphone"  > </  ASP: regularexpressionvalidator  >         </  P  >              <  ASP: button  ID  = "Btnsubmit"  Runat  = "Server"  Text  = "Submit"    />          </  Form  >  </  Body >  </  Html  > 

 

If the entered format is not an email or mobile phone number, an error message is displayed:

 

Note: unless the browser does not support client verification or enableclientscript is set to false, server-side and client-side verification will be performed simultaneously.
Note: If the input control is empty, verification fails. Use the requiredfieldvalidator control to make the field a required field.

The following are common Regular Expressions for your reference:

Verification number: ^ [0-9] * $

Verify the n-digit number: ^ \ D {n} $

Verify at least N digits: ^ \ D {n,} $

Verify M-N digits: ^ \ D {m, n} $

Verify the number starting with zero or zero: ^ (0 | [1-9] [0-9] *) $

Verify the positive number of two decimal places: ^ [0-9] + (. [0-9] {2 })? $

Verify the positive number of 1-3 decimal places: ^ [0-9] + (. [0-9] {1, 3 })? $

Verify a non-zero positive integer: ^ \ +? [1-9] [0-9] * $

Verify a non-zero negative integer: ^ \-[1-9] [0-9] * $

Verify non-negative integer (positive integer + 0) ^ \ D + $

Verify non-positive integer (negative integer + 0) ^ (-\ D +) | (0 +) $

3 characters for verification: ^. {3} $

Verify a string consisting of 26 English letters: ^ [A-Za-Z] + $

Verify a string consisting of 26 uppercase letters: ^ [A-Z] + $

Verify a string consisting of 26 lower-case letters: ^ [A-Z] + $

Verify a string consisting of digits and 26 English letters: ^ [A-Za-z0-9] + $

Verify a string consisting of digits, 26 English letters, or underscores: ^ \ W + $

Often used to verify the user name or nickname: ^ [\ u4e00-\ u9fa5A-Za-z0-9-_] * $ only Chinese and English, numbers, underscores, minus signs

Verify User Password: ^ [A-Za-Z] \ W {5, 17} $ the correct format is: it must start with a letter and be between 6 and 18 characters. It can only contain characters, numbers, and underscores.

Check whether ^ % & ',; =? $ \ "And other characters: [^ % & ',; =? $ \ X22] +

Verify Chinese characters: ^ [\ u4e00-\ u9fa5], {0,} $

Verify email address: ^ \ W + [-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * $

Verify interneturl: ^ http: // ([\ W-] + \.) + [\ W-] + (/[\ W -./? % & =] *)? $; ^ [A-Za-Z] +: // (W + (-W +) *) (. (W + (-W + )*))*(? S *)? $

Verification 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 the ID card number (15 or 18 digits): ^ \ D {15} | \ D {} 18 $

12 months of verification: ^ (0? [1-9] | 1 [0-2]) $ the correct format is: "01"-"09" and "1" "12"

31 days of verification for a month: ^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $ the correct format is: 01, 09, 1, 31.

Non-negative floating point number (Positive floating point number + 0): ^ \ D + (\. \ D + )? $

Positive floating point number ^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $

Non-Positive floating point number (negative floating point number + 0) ^ (-\ D + (\. \ D + )?) | (0 + (\. 0 + )?)) $

Negative floating point number ^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $

Floating Point Number ^ (-? \ D +) (\. \ D + )? $

Regular Expression -- verify mobile phone number: 13 [0-9] {9}

Extract the network link in the information: (H | H) (R | r) (E | E) (F | f) * = * ('| ")? (\ W | \/| \.) + ('| "| * |> )?

Extract the image link in the information: (S | S) (R | r) (C | C) * = * ('| ")? (\ W | \/| \.) + ('| "| * |> )?

Extract the IP address in the information: (\ D +) \. (\ D +)

Extract the Chinese mobile phone number from the information: (86) * 0*13 \ D {9}

Extracted Chinese landline numbers from the information: (\ D {3, 4} \) | \ D {3, 4}-| \ s )? \ D {8}

Extract Chinese phone numbers (including mobile and landline phones) from the Information: (\ D {3, 4} \) | \ D {3, 4}-| \ s )? \ D {7, 14}

Extracted Chinese zip code: [1-9] {1} (\ D +) {5}

Chinese ID card number in the extracted information: \ D {18} | \ D {15}

Extract the integer \ D + from the information.

Extract any number from the information :(-? \ D *) (\. \ D + )?

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.