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?
62413-Oct-2021
Updated on 13-Oct-2021
Home / DeveloperSection / Forums / 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?
Ravi Vishwakarma
14-Oct-2021A 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
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
And then use annotation validation like
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
And put name and value, and then save it. Then go to your model import this namespace.
Like using [using yourprojectname.Properties]
eg
Next use annotation validation for email
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.
it's showing the message from the resource file because the email doesn't contain the space.
Thank you.