---
title: "How to use common message in the regex pattern from the resource file in asp.net MVC?"  
description: "How to use common message in the regex pattern from the resource file in asp.net MVC?"  
author: "Ashutosh Kumar Verma"  
published: 2021-10-13  
updated: 2021-10-13  
canonical: https://www.mindstick.com/forum/156781/how-to-use-common-message-in-the-regex-pattern-from-the-resource-file-in-asp-dot-net-mvc  
category: "asp.net"  
tags: ["c#", "asp.net", "asp.net mvc", "asp.net core"]  
reading_time: 3 minutes  

---

# How to use common message in the regex pattern from the resource file in asp.net MVC?

How to use [common](https://www.mindstick.com/articles/23170/10-most-common-accounting-mistakes-of-small-business) [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) in the [regex pattern](https://www.mindstick.com/forum/1663/replacing-link-tag-using-regex) from the [resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) [file in asp.net](https://www.mindstick.com/forum/158972/how-to-read-appsettings-values-from-a-json-file-in-asp-dot-net-core) MVC?

## Replies

### Reply by Ravi Vishwakarma

A regular expression can also be described by a sequence of patterns that define a string. Regular expressions are used to match character combinations in strings. This [pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-pattern) is used by the string searching algorithm to find the operations in the string.

## Let’s discuss the topic

First of all, create an [asp.net MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) project name as **WebApplication1**

![How to use common message in the regex pattern from the resource file in asp.net MVC?](https://www.mindstick.com/mindstickforums/2d7dfafb-8e20-4933-8153-dd137887bb8f/images/8278a10d-1f23-423f-80fc-b8368cc8462a.png)\

Next, Create a model according to your personality, goto model folder -> right-click -> add button -> class, take class name **ResetPassword** model We are creating a ResetPassword model\

![How to use common message in the regex pattern from the resource file in asp.net MVC?](https://www.mindstick.com/mindstickforums/2d7dfafb-8e20-4933-8153-dd137887bb8f/images/d92c6c37-2f1e-4786-afdd-f9cdaf6306bd.png)\

## And then use annotation validation like

```
[[Required(AllowEmptyStrings = false, ErrorMessage = 'Can't empty email ')]], [DataType(DataType.Password)],
[Compare('Password',”Password doesn’t match”)]
[StringLength(50, MinimumLength =3)] etc.
```

![How to use common message in the regex pattern from the resource file in asp.net MVC?](https://www.mindstick.com/mindstickforums/2d7dfafb-8e20-4933-8153-dd137887bb8f/images/54295f69-941e-43fd-b726-ddf5d2bd93a5.png)\

In long project use, validation message but everywhere can’t put the same message. So, we use the [resource file](https://www.mindstick.com/articles/327263/what-is-the-use-of-resource-file-how-to-use-it-on-your-project) for storing the messages. The resource file stores value in key-value pair. In resource files, we can store images, strings, text files, etc. We create the resource file in Visual Studio 2019 **Go to project -> right-click -> click properties -> Click on Resource button -> select string in header bar**

![How to use common message in the regex pattern from the resource file in asp.net MVC?](https://www.mindstick.com/mindstickforums/2d7dfafb-8e20-4933-8153-dd137887bb8f/images/61bad0a4-b597-4423-9b53-d5f2ec1df007.png) **\**

**And put name and value, and then save it. Then go to your model import this namespace.**

## Like using [using yourprojectname.Properties]

eg

> **using WebApplication1.Properties;**

```
Here is CommanRegex
 class for string the all regex pattern
```

```
using System;
using System.Text.RegularExpressions;
namespace HelloWorldMVC.Comman{
    public class CommanRegex
    {
        public const string EmailRegex = '^[\\w!#$%&’*+/=?`{|}~^-]+(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$';
        public const string PasswordRegex = @'^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&-+=()])(?=\S+$).{8,20}$';
        public const string FirstNameRegex = @'\w{3,100}';
        public const string LastNameRegex = @'\w{3,100}';
        public const string ContactNoRegex = @'\d{10}';
        public static bool IsValidFirstName(string name)        {
            return Regex.IsMatch(name, FirstNameRegex);
        }
        public static bool IsValidPassword(string password)        {
            return Regex.IsMatch(password, PasswordRegex);
        }
    }
}
```

**Next use annotation validation for email**\

```
[Display(Name = 'Email ')]
[Required(AllowEmptyStrings = false, ErrorMessage = 'Can't empty email ')] // without resource file message [RegularExpression(pattern: CommanRegex.EmailRegex, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = 'regexEmail')] // from resource file meaasge public string Email { get; set; }
```

In RegularExpression annotation has to take [regex](https://www.mindstick.com/forum/160352/how-to-use-regex-in-javascript-for-pattern-matching) pattern and message

## ErrorMessageResourceType = typeof(Resources) -> it is used to identify the resource file name

**ErrorMessageResourceName = 'regexEmail' -> it is used to identify the messahe name from resource file**

and create your view and run your project. we can see that validation has been successful.

![How to use common message in the regex pattern from the resource file in asp.net MVC?](https://www.mindstick.com/mindstickforums/2d7dfafb-8e20-4933-8153-dd137887bb8f/images/d0c36bd1-41c6-48ef-bbe7-de178d99a03f.png)

it's showing the message from the resource file because the email doesn't contain the space.

Thank you.


---

Original Source: https://www.mindstick.com/forum/156781/how-to-use-common-message-in-the-regex-pattern-from-the-resource-file-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
