Click here to Skip to main content
15,907,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ?

What I have tried:

Have searched but didn't get correctly how to do this
Posted
Updated 24-May-17 1:27am

1 solution

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900