Regular expressions, also referred to as regex or regexp, provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. Typical uses of regular expressions include validating user input of email addresses, phone numbers, credit card numbers, and so on, but can be much more complex.
-
In Visual Studio, select
RegularExpressionValidator
from the Toolbox (Validation category). -
In the webform, add the
RegularExpressionValidator
control and bind it to the Text input field to which you want to add validation.For example, you want to make sure the end-user enters a valid email address and phone number. You would need to add two regex validator controls (
REV_Email
andREV_Phone
) and bind each of them to the associated TextBoxes (email
andphone
). -
In the regex validator properties, set the following values:
-
ControlToValidate = <the name of the textbox>
-
Display = None
-
Error Message = <a user-friendly error message>
-
SetFocusOnError = True
-
ValidationGroup = WFGENPage
-
ValidationExpression = <the RegEx string>
These settings will combine the error message to WorkflowGen page validation if the input text does not match the criteria.
-
For the ValidationExpression
property, click on the ellipsis (...
) button on that property field and a dialog box will appear.
Visual Studio already includes some commonly used basic regular expressions. For example:
- Internet e-mail address
- Internet URL
- U.S. phone number
- U.S. Social Security number
- U.S. ZIP code
You can also create your own regex string using Custom or select among the following samples below:
Usage | RegEx |
---|---|
Email address | \b[A-Z0-9.\_%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b^\w+([\.-]?\w+)\*@\w+([\.-]?\w+)\*(\.\w{2,3})+$ |
Phone number | ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}^\(?\d{3}\)?[\s\-]?\d{3}\-?\d{4}$ |
US Social Security number | \d{3}-\d{2}-\d{4} |
US ZIP code | ^\d{5}(\-\d{4})?$ |
Canada postal code | ^\s\*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s\*$ |
IP address | \b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b |
MAC address | ^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$ |
Domain name | ^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$ |
URL | ^(http[s]?://|ftp://)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)$ |
Date in yyyy-MM-dd format |
(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01]) |
Date in MM-dd-yyyy format |
^([\d]|1[0,1,2])/([0-9]|[0,1,2][0-9]|3[0,1])/\d{4}$ |
Time in 24-hour format | ^(20|21|22|23|[01]d|d)(([:][0-5]d){1,2})$ |
Roman number | ^(?i:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$ |
Float number | [-+]?(?:\b[0-9]+(?:\.[0-9]\*)?|\.[0-9]+\b)(?:[eE][-+]?[0-9]+\b)? |
Dollar amount with leading $
|
\$[0-9]\*.[0-9][0-9] |
Decimal number | ^\d\*[0-9](\.\d\*[0-9])?$ |
Document filenames | ^[a-zA-Z0-9-\_\.]+\.(pdf|txt|doc|csv)$ |
VISA | ^4[0-9]{12}(?:[0-9]{3})?$ |
MasterCard | ^5[1-5][0-9]{14}$ |
American Express | ^3[47][0-9]{13}$ |
Diners Club | ^3(?:0[0-5]|[68][0-9])[0-9]{11}$ |
Discover | ^6(?:011|5[0-9]{2})[0-9]{12}$ |
JCB | ^(?:2131|1800|35\d{3})\d{11}$ |
General credit card number | ^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$ |
References
http://support.microsoft.com/kb/308252
http://regexlib.com/DisplayPatterns.aspx?cattabindex=2categoryId=3
http://tools.netshiftmedia.com/regexlibrary/
http://www.regular-expressions.info/examples.html
http://myregexp.com/examples.html
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/