[Resource] Regular Expression-. NET2.0

Source: Internet
Author: User

This article implements:
Only one number can be entered
Only n numbers can be entered
You can enter at least n numbers.
Only m to n numbers can be entered
Only numbers can be entered
Only numbers in a certain range can be entered.
Only numbers with 0 and non-0 headers can be entered.
Only real numbers can be input.
Only the positive number of n decimal places can be entered
Only the positive number of m-n decimal places can be entered
Only positive integers other than 0 can be entered.
Only negative integers other than 0 can be entered.
Only n characters can be entered
Only English characters can be entered
Only uppercase English characters can be entered
Only lowercase English characters can be entered
Only English characters and numbers can be entered
Only English characters, numbers, and underscores can be entered.
Password example
Upper-case verification letter
Verify the URL (? Id = Chinese) VS. NET2005 does not have this function
Verify Chinese Characters
Verify QQ number
Verify email (verify the same MSN number)
Verify the ID card number (for rough verification, it is best to tune the class library on the server side for further verification)
Verify the mobile phone number (including 159, excluding PHS)
Verify the phone number (very complicated, VS. NET2005 gave the error)
Verify passport
Verification IP address, verification domain verification Credit Card (support for Visa card, MasterCard, Discover Card, US Express Card)
Verify ISBN international standard document No.
Globally Unique GUID Verification
Verify the file path and extension
Verify the Html color value

The regular expression is a. NET invisible syntax. There are many articles about its syntax. You can also refer to MSDN. The purpose of this article is to give an example, including all regular expression usage, which will be constantly supplemented. You are welcome to leave a message to provide support!

RegularExpressionValidator Control
Use the RegularExpressionValidator control. Of course, you do not need to use the RegularExpressionValidator Control for direct verification in the background.
The ValidationExpression property comes with several default verification methods, but that is far from enough. The ErrorMessage attribute is an error message.

 

You can click the "..." pop-up window to select the regular expression you want, or directly write it by yourself.

When the input in IE is not in the corresponding format, an error is reported:


Verification Number:
Only one number can be entered

 
Expression ^ \ d $
The description matches a number.
Matching example 0, 1, 2, 3
Example of Mismatch

Only n numbers can be entered

 
Expression ^ \ d {n} $ for example ^ \ d {8} $
The description matches 8 numbers.
Matching example: 12345678,22223334, 12344321
Example of Mismatch

You can enter at least n numbers.

 
Expression ^ \ d {n ,}$ for example ^ \ d {8,} $
The description matches at least n numbers.
Matching example: 12345678,123456789, 12344321
Example of Mismatch

Only m to n numbers can be entered

 
Expression ^ \ d {m, n }$ for example ^ \ d {7, 8} $
The description matches m to n numbers.
Matching example: 12345678,1234567
Example of mismatched 123456,123456789

Only numbers can be entered

 
Expression ^ [0-9] * $
The description matches any number.
Matching example: 12345678,1234567
Unmatched example E, clear month, http://blog.csdn.net/21aspnet

Only numbers in a certain range can be entered.

 
Expression ^ [12-15] $
Describe the number that matches a certain range
Matching example :,
Example of Mismatch

Only numbers with 0 and non-0 headers can be entered.

 
Expression ^ (0 | [1-9] [0-9] *) $
The description can be 0, the first number cannot be 0, and the number can contain 0
Matching example 101,100
Unmatched example 01, clear month, http://blog.csdn.net/21aspnet

Only real numbers can be input.

 
Expression ^ [-+]? \ D + (\. \ d + )? $
Description matching real number
Example 18, + 3.14,-9.90
Example of mismatched. 6, 33 S, 67-99

Only the positive number of N decimal places can be entered

 
Expression ^ [0-9] + (. [0-9] {n })? $ With ^ [0-9] + (. [0-9] {2 })? $ Example
Describe the positive number matching n decimal places
Example of matching 2.22
Unmatched example 2.222,-2.22, http://blog.csdn.net/21aspnet

Only the positive number of M-N decimal places can be entered

 
Expression ^ [0-9] + (. [0-9] {m, n })? $ With ^ [0-9] + (. [0-9] {1, 2 })? $ Example
Describe the positive number of M to N decimal places
Matching examples 2.22, 2.2
Unmatched example 2.222,-2.2222, http://blog.csdn.net/21aspnet

Only positive integers other than 0 can be entered.

 
Expression ^ \ +? [1-9] [0-9] * $
Describe to match a positive integer other than 0
Matching example 2, 23, 234
Example 0,-4,

Only negative integers other than 0 can be entered.

 
Expression ^ \-[1-9] [0-9] * $
Describe to match a non-0 negative integer
Example of matching-2,-23 and-234
Examples of unmatched values 0, 4,

Only n characters can be entered

 
The expression ^. {n} $ takes ^. {4} $ as an example.
The description matches n characters. Note that the Chinese character is only one character.
Matching example 1234, 12we, 123 Clear, clear moon
Unmatched example 0,123,123 www, http://blog.csdn.net/21aspnet/

Only English characters can be entered

 
Expression ^. [A-Za-z] + $
The description matches English characters and is case-insensitive.
Example of matching Asp, WWW,
Unmatched example 0,123,123 www, http://blog.csdn.net/21aspnet/

Only uppercase English characters can be entered

 
Expression ^. [A-Z] + $ as an Example
Description matching uppercase English characters
Matching example NET, WWW,
Mismatched example 0,123,123 www,

Only lowercase English characters can be entered

 
Expression ^. [a-z] + $
Description matching uppercase English characters
Example of matching asp and csdn
Example 0, NET, WWW,

Only English characters and numbers can be entered

 
Expression ^. [A-Za-z0-9] + $ as an Example
Description matching English characters + numbers
Example of matching: 1Asp, W1W1W,
Unmatched example 0,123,123, www, http://blog.csdn.net/21aspnet/

Only English characters, numbers, and underscores can be entered.

 
The expression ^ \ w + $ is used as an example.
The description matches English characters, numbers, and underscores.
Example of matching: 1Asp, WWW, 12,1_w
Unmatched Example 3 #, 2-4, w # $, http://blog.csdn.net/21aspnet/

Password example

 
Expression ^. [a-zA-Z] \ w {m, n} $
The description matches the m-n characters starting with an English character and can only contain numbers, letters, and underscores.
Matching example
Example of Mismatch

Upper-case verification letter
 
Expression \ B [^\ Wa-z0-9 _] [^ \ WA-Z0-9 _] * \ B
The first letter of the description can only be capitalized.
Example of matching Asp, Net
Unmatched example http://blog.csdn.net/21aspnet/

Verify the URL (? Id = Chinese) VS. NET2005 does not have this function
 
Expression ^ http: \/([\ w-] + (\. [\ w-] +) + (\/[\ w -.\/\? % & = \ U4e00-\ u9fa5] *)? $
 
Description verification tape? Id = Chinese
Matching example http://blog.csdn.net/21aspnet,
Http://blog.csdn.net? Id = clear moon
Example of Mismatch

Verify Chinese Characters
 
Expression ^ [\ u4e00-\ u9fa5] {0,} $
Description can only contain Chinese Characters
Matching example: Clear moon
Unmatched example http://blog.csdn.net/21aspnet/

Verify QQ number
 
Expression [0-9] {5, 9}
Description: QQ number of 5-9 digits
Example of matching
Unmatched example 10000 W, http://blog.csdn.net/21aspnet/

Verify email (verify the same MSN number)
Expression \ W + ([-+. '] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W + )*
Description Note: MSN can use a non-hotmail.com mailbox.
Matching example aaa@msn.com
Example of mismatched 111 @ 1. http://blog.csdn.net/21aspnet/

Verify the ID card number (for rough verification, it is best to tune the class library on the server side for further verification)
 
Expression ^ [1-9] ([0-9] {16} | [0-9] {13}) [xX0-9] $
Description
For example, if the ID card number is 15 or 18 digits, the ID card number with X is supported.
Unmatched example http://blog.csdn.net/21aspnet/

Verify the mobile phone number (including 159, excluding PHS)
 
Expression ^ 13 [0-9] {1} [0-9] {8} | ^ 15 [9] {1} [0-9] {8}
The description contains the mobile phone number 159-130.
Matching example 139 XXXXXXXX
Unmatched example 140 XXXXXXXX, http://blog.csdn.net/21aspnet/

Verify the phone number (very complicated, VS. NET2005 gave the error)
 
Expression (imperfect) solution 1 (\ d {3} \) | \ d {3}-) | (\ d {4 }\) | \ d {4 }-))? (\ D {8} | \ d {7 })
Solution 2 (^ [0-9] {3, 4} \-[0-9] {3, 8} $) | (^ [0-9] {3, 8} $) | (^ \ ([0-9] {3, 4} \) [0-9] {3, 8} $) | (^ 0 {0, 1} 13 [0-9] {9} $) mobile phone numbers are supported but not perfect.
Description Shanghai: 02112345678 3 + 8 digits
Shanghai: 021-12345678
Shanghai: (021)-12345678
Shanghai: (021) 12345678
Zhengzhou: 03711234567 4 + 7 places
Hangzhou: 057112345678 4 + 8 places
There is also an extension number, country code.
Because the situation is very complex, we do not recommend that the front-end do 100% verification. So far, no one seems to be able to write a file containing all types. In fact, many situations are in conflict.
If anyone has a better phone number, leave a message.
 
Matching example
Example of Mismatch

Verify passport
 
Expression (P \ d {7}) | G \ d {8 })
 
Description verification P + 7 digits and G + 8 digits
Matching example
Examples of mismatched clear moon, http://blog.csdn.net/21aspnet/

Verify IP Address
 
Expression ^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) $
 
Description verification IP Address
Example of matching: 192.168.0.1 222.234.1.4
Example of Mismatch

Verification domain
Expression ^ [a-zA-Z0-9] + ([a-zA-Z0-9 \-\.] + )? \. (Com | org | net | cn | com.cn | edu.cn | grv.cn |) $
 
Description verification domain
Example of matching csdn.net baidu.com it.com.cn
Example of mismatched 192.168.0.1

Verify credit card
Expression ^ ((? : 4 \ d {3}) | (? : 5 [1-5] \ d {2}) | (? : 6011) | (? : 3 [68] \ d {2}) | (? : 30 [012345] \ d) [-]? (\ D {4}) [-]? (\ D {4}) [-]? (\ D {4} | 3 [4, 7] \ d {13}) $
 
Description verification: Visa, MasterCard, Discover, and American Express Card
Matching example
Example of Mismatch

Verify ISBN international standard document No.
Expression ^ (\ d [-] *) {9} [\ dxX] $
 
Description verification ISBN International Standard No.
Example of matching 7-111-19947-2
Example of Mismatch

Globally Unique GUID Verification
Expression ^ [A-Z0-9] {8}-[A-Z0-9] {4}-[A-Z0-9] {4}-[A-Z0-9] {4}-[A-Z0-9] {12} $
 
Description format: 8-4-4-4-12
Example of matching: 2064d355-c0b9-41d8-9ef7-9d8b26524751
Example of Mismatch

Verify the file path and extension
Expression ^ ([A-Za-Z] \: |\\) \ ([^ \] + \) * [^ \/:*? "<> |] + \. Txt (l )? $
 
Description check path and file extension
Matching example E: \ mo.txt
Example of mismatch E: \, mo.doc, E: \ mo.doc, http://blog.csdn.net/21aspnet/

Verify the HTML color value
Expression ^ #? ([A-F] | [A-F] | [0-9]) {3} ([A-F] | [A-F] | [0-9]) {3 })? $
 
Description Check color value
Matching example # ff0000
Unmatched example http://blog.csdn.net/21aspnet/

 

This article from: webmaster sharing station (www.adminfx.cn) Detailed reference: http://www.adminfx.cn/article/jishu/Program/20070927/743.html


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.