Validate Input Using Regular Expressions

Problem :

You need to validate that user input or data read from a file has the expected structure and content.
For example, you want to ensure that a user enters a valid IP address, telephone number, or e-mail

Solution :

Use regular expressions to ensure that the input data follows the correct structure and contains only
valid characters for the expected type of information.

How It Works :

When a user inputs data to your application or your application reads data from a file, it’s good
practice to assume that the data is bad until you have verified its accuracy. One common validation
requirement is to ensure that data entries such as e-mail addresses, telephone numbers, and credit
card numbers follow the pattern and content constraints expected of such data. Obviously, you cannot
be sure the actual data entered is valid until you use it, and you cannot compare it against values that are known to be correct. However, ensuring the data has the correct structure and content is
a good first step to determining whether the input is accurate. Regular expressions provide an excellent
mechanism for evaluating strings for the presence of patterns, and you can use this to your
advantage when validating input data.
The first thing you must do is figure out the regular expression syntax that will correctly match
the structure and content of data you are trying to validate. This is by far the most difficult aspect of
using regular expressions. Many resources exist to help you with regular expressions, such as
The Regulator ( and RegExDesigner.NET by Chris Sells The web site ( also provides hundreds of useful prebuilt expressions.
Regular expressions are constructed from two types of elements: literals and metacharacters.
Literals represent specific characters that appear in the pattern you want to match. Metacharacters
provide support for wildcard matching, ranges, grouping, repetition, conditionals, and other control
mechanisms. Table 2-2 describes some of the more commonly used regular expression metacharacter
elements. (Consult the .NET SDK documentation for a full description of regular expressions.)


Input Type


Regular Expression

Numeric input

The input consists of one or more decimal digits; for example,
5 or 5683874674.


Personal identification number (PIN)

The input consists of four decimal


Credit card number

The input consists of data that matches the pattern of most major
credit card numbers; for example,
4921835221552042 or


Simple password

The input consists of six to eight characters; for example, ghtd6f
or b8c7hogh.





E-mail address

The input consists of an Internet expression
indicates that each address element
must consist of one or more word
characters or hyphens; for example,





The input consists of an HTTP-based or HTTPS-based URL; for example,


The Code :
The ValidateInput method shown in the following example tests any input string to see if it
matches a specified regular expression.

using System;
using System.Text.RegularExpressions;

namespace mahmoud_alam
    class new292
        public static bool ValidateInput(string regex, string input)
            // Create a new Regex based on the specified regular expression.
            Regex r = new Regex(regex);
            // Test if the specified input matches the regular expression.
            return r.IsMatch(input);
        public static void Main(string[] args)
            // Test the input from the command line. The first argument is the
            // regular expression, and the second is the input.
            Console.WriteLine("Regular Expression: {0}", args[0]);
            Console.WriteLine("Input: {0}", args[1]);
            Console.WriteLine("Valid = {0}", ValidateInput(args[0], args[1]));
            // Wait to continue.
            Console.WriteLine("\nMain method complete. Press Enter");


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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s