athira k

athira k

  • NA
  • 140
  • 53.4k

How to give client side validation MVC

May 24 2017 1:40 PM
I have given validation on server side but I need validation on typing itself. It must show the error message when typing is wrong.
controller
 
if (!string.IsNullOrEmpty(student.FirstName))
{
string nameRegex = @"^([a-zA-Z ])+$";
Regex re = new Regex(nameRegex);
if (!re.IsMatch(student.FirstName))
{
ModelState.AddModelError("FirstName", "FirstName only be letters");
}
}
else
{
ModelState.AddModelError("FirstName", "FirstName is required");
}
 

I have given validation in regular expression Like this in controller part

I need to give validation for name that it should accept only letters and numbers and no special characters

model
 
public string FirstName { get; set; } 
 
how can I do this ?? can anyone please help me to do this ? 
 
 

Answers (4)