Regular Expression Attribute in ASP.NET MVC

Regular Expression Attribute in ASP.NET MVC

In this article, I am going to discuss the Regular Expression Attribute in ASP.NET MVC application with some real time examples. The Regular Expression Attribute is generally used for pattern matching validations in ASP.NET MVC application. Please read the following articles before proceeding to this article as we are going to work with the same example that we started in our previous articles.

Data Annotations in ASP.NET MVC

Required and StringLength Attribute

Let’s understand Regular expression attribute with an example.

Suppose, we need to validate the Email ID of an employee, then we can achieve this very easily in ASP.NET MVC application by using Regular expression attributes as shown below: 

[Required(ErrorMessage = "Email id is required")]
[RegularExpression(@"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z",
                            ErrorMessage = "Please enter a valid email address")]
public string EmailId
{
    get;
    set;
}

In the above example, we are applying both Required and RegularExpression attribute to the EmailID model property which ensures that the Email Id is a required field and along with it will validate the email id field value as shown below.

When a user tries to enter an invalid Email ID and submit the page, then he will get the validation error message as shown below:

Regular Expression Attribute in ASP.NET MVC

Let’s see another example of

Here is the requirement for validating Name property

1. UserName can contain the first and last name with a single space. 

2. The last name is optional. If the last name is not present, then there shouldn’t be any space after the first name.

3. Only upper and lower case alphabets are allowed.

This requirement can achieve very easily in ASP.NET MVC using RegularExpressionAttribute. In Employee.cs class file, decorate UserName property with RegularExpression attribute as shown below.

[RegularExpression(@"^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$")]
public string UserName
{
    get;
    set;
}

Notice that, we are passing regular expression string to the attribute constructor. The Regular Expression Attribute is great for pattern matching and ensures that the value for UserName property is in the format that we want. Also, notice that we are using a verbatim literal (@ symbol) string, as we don’t want escape sequences to be processed.

Run the application and see UserName field is work as expected.

Understanding and writing regular expressions is beyond the scope of this article. If you are interested in learning to write regular expressions, here is a link from MSDN

http://msdn.microsoft.com/en-us/library/az24scfc.aspx

The following website is very helpful, for writing and testing regular expressions. This website also contains commonly used regular expressions. In fact, I have picked up the regular expression for validating UserName property from here.

http://gskinner.com/RegExr/

In the next article, I will discuss Range Attribute in ASP.NET MVC application,

SUMMARY

In this article, I try to explain the Regular Expression Attribute in ASP.NET MVC application step by step with some real-time examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article. 

Leave a Reply

Your email address will not be published. Required fields are marked *