Use Regular Expressions to Constrain Input in ASP.NET

ow To shows how you can use regular expressions within ASP.NET applications to constrain untrusted input. Regular expressions are a good way to validate text fields such as names, addresses, phone numbers, and other user information. You can use them to constrain input, apply formatting rules, and check lengths. To validate input captured with server controls, you can use the System.Text.RegularExpressions.Regex class.

Use the Regex class to constrain and validate input.

Regular expression support is available to ASP.NET applications through theRegularExpressionValidator control and the Regex class in the System.Text.RegularExpressionsnamespace.

Using the Regex Class

If you are not using server controls (which means you cannot use the validation controls) or if you need to validate input from sources other than form fields, such as query string parameters or cookies, you can use the Regex class within the System.Text.RegularExpressions namespace.

To use the Regex class

  1. Add a using statement to reference the System.Text.RegularExpressions namespace.
  2. Call the IsMatch method of the Regex class, as shown in the following example.
    // Instance method:
    Regex reg = new Regex(@"^[a-zA-Z'.]{1,40}$");
    // Static method:
    if (!Regex.IsMatch(txtName.Text, 
      // Name does not match schema
    For performance reasons, you should use the static IsMatch method where possible to avoid unnecessary object creation

Use Regular Expression Comments

Regular expressions are much easier to understand if you use the following syntax and comment each component of the expression by using a number sign (#). To enable comments, you must also specifyRegexOptions.IgnorePatternWhitespace, which means that non-escaped white space is ignored.

Regex regex = new Regex(@"
                        ^           # anchor at the start
                       (?=.*\d)     # must contain at least one numeric character
                       (?=.*[a-z])  # must contain one lowercase character
                       (?=.*[A-Z])  # must contain one uppercase character
                       .{8,10}      # From 8 to 10 characters in length
                       \s           # allows a space 
                       $            # anchor at the end", 

Basic things to be understood in RegEx: 

"*" matches 0 or more patterns
"?" matches single character
"^" for ignoring matches.
"[]" for searching range patterns.

I am giving You some Pattern Samples For Validations

Pattern 1 For alpha

You Can make these in class or can acces on your page directly

I am providing you example with class with  a static method

Namspace OrientPatterns


Public Class OrientExpressions


public static bool IsAlpha(String s)



return true;


 return falsel;




Now You can acces this method your page on button click with class name without create object of class

Here are sampe pattern Samples which will very usefull for you

Pattern 1 For Numerics


Pattern 2 For Alphas


Pattern 3 For Password


. Match any character except newline
\w Match any alphanumeric character
\s Match any whitespace character
\d Match any digit
\b Match the beginning or end of a word
^ Match the beginning of the string
$ Match the end of the string

Be happy and enjoy in Programming !!!!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s