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
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
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
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);
}
}
}
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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
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.