Loading

ASP.NET MVC

What is Regular Expression Attribute in ASP.NET MVC?. The Complete ASP.NET MVC Developer Course 2023 [Videos].

In this article, I am going to discuss the Regular Expression Attribute in ASP.NET MVC application with examples. Please read our previous article where we discussed Required and StringLength Attribute in ASP.NET 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 applications.

Lets 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 the 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])?)", ErrorMessage = "Please enter a valid email address")] public string EmailId { get; set; }

In the above example, we are applying both Required and RegularExpression attributes 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 shouldnt 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 the Employee.cs class file, decorate the UserName property with the 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 the UserName property is in the format that we want. Also, notice that we are using a verbatim literal (@ symbol) string, as we dont 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 Video. 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 the UserName property from here.

http://gskinner.com/RegExr/

See All

Comments (300 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /300

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /300

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /300