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 MVC application with real-time examples. Please read our previous article where we discussed Required and StringLength Attribute in MVC application. At the end of this article, you will understand What are Regular Expression Attribute and how and when to use Regular Expression Attribute in ASP.NET MVC application with examples.

What is Regular Expression Attribute in ASP.NET MVC?

The Regular Expression Attribute is generally used for pattern matching validations in ASP.NET MVC application.

Let’s understand the 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")]
                            ErrorMessage = "Please enter a valid email address")]
public string EmailId

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

Another Example:

Here is the requirement for validating Name property

  1. UserName can contain the first and last names 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.

public string UserName

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 the 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

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.

In the next article, I am going to discuss Range Attribute in ASP.NET MVC Application. Here, 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 Regular Expression Attribute in MVC article. 

2 thoughts on “Regular Expression Attribute in ASP.NET MVC”

  1. blank

    Hi there . You have done a great work by writing these articles in a customized way. It is very easy to understand. Thanks . I have read all the articles till now. (Every single one of it).

Leave a Reply

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