How to use common message in the regex pattern from the resource file in asp.net MVC?

How to use common message in the regex pattern from the resource file in asp.net MVC?

Last updated:10/13/2021 3:09:21 AM

1 Answers

Ravi Vishwakarma
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 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 project name as WebApplication1

How to use common message in the regex pattern from the resource file in asp.net MVC?

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?

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?

In long project use, validation message but everywhere can’t put the same message. So, we use the resource file 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?

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

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

Thank you.

Answer