Monday, April 15, 2013

Web Server Control Regular Expression Validator

RegularExpressionValidator is one of most useful validation controls which automatically provides both client-side and server-side validation. It is simple and straigforward to be invoded. All what you need it to supply an appropriated regular expression. And the web server control will hanlde both client-side and server-side validations for you. As long as you are familiar with the way to construct a regular expression, you will be able to validate lots of inputs by using RegularExpressionValidator. For instance, Email, URL, Phone #, Postal Code, SIN, Strong Password and etc...
Web Server Control: RegularExpressionValidator
NameSpace: System.Web.UI.WebControls

Metacharacters for Matching Type of Characters
Character Class Description
. Matches any character except \n
[aeiou] Matches any single character specified in the set
[^aeiou] Matches any character not specified in the set
[3-7a-dA-D] Matches any character specified in the specified ranges
\w Matches any word character; that is, any alphanumeric character or the underscore.
\W Matches any nonword character
\s Matches any whitespace (space, tab, form feed, newline, carriage return, or vertical feed).
\S Matched any nonwhitespace character
\d Matches any decimal character
\D Matched any nondecimal character
Metacharacters for Matching Single Characters
Character Escapes Description
Ordinary characters Characters other than .$^{[(|)*+?\
\b Matches a backspace
\t Matches a tab
\r Matches a carriage return
\v Matches a vertical tab
\n Matches a newline
\f Matches a form feed
\ If followd by a special character (one of .$^{[(|)*+?\), this character escape matches that character leteral.
Quantifiers
Quantifier Description
* Zero or more matches
+ One or more matches
? Zero or one matches
{N} N matches
{N,} N or more matched
{N,M} Between N and M matches (inclusive)
Demo #1: Validate Canadian Postal Code: [a-zA-Z]\d[a-zA-Z]\s?\d[a-zA-Z]\d"

   

Demo #2: Validate USA Zip Code: \d{5}(-\d{4})?

   

Demo #4: Validate Internet Email Address: \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

   

Demo #5: Validate Internet URL: ([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

   

Demo #6: Validate North America Phone Number: \d{3}-?\d{3}-?\d{4}

   

Demo #7: Validate USA Social Security Number: \d{3}-?\d{2}-?\d{4}

   

Demo #8: Validate Canadian Social Insurance Number: \d{3}-?\d{3}-?\d{3}

   

No comments:

Post a Comment