如何给客户端验证MVC [英] How to give client side validation MVC
问题描述
我已经在服务器端进行了验证,但我需要验证输入本身。输入错误时必须显示错误信息。
控制器
if(!string.IsNullOrEmpty(学生) .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);
}
我在正则表达式中给出了验证在控制器部分中这样的确认
我需要验证名称,它应该只接受字母和数字而不是特殊字符
model
public string FirstName {get;组; }
我该怎么做?有人可以帮我这么做吗?
我尝试了什么:
已搜索但未正确完成如何操作
;
Regex re = new Regex(nameRegex);
if(!re.IsMatch(student.FirstName))
{
ModelState.AddModelError(FirstName,FirstName only be letters);
}
}
else
{
ModelState.AddModelError(FirstName,FirstName is required);
}
我已经在正则表达式中给出了验证在控制器部分中这样做
我需要对名称进行验证,它只接受字母和数字而不是特殊字符
型号
公共字符串FirstName {get; set;}
怎么可以我这样做?有人可以帮我这么做吗?
我尝试了什么:
已搜索但未正确完成此操作
mvc客户端验证正则表达式 - Google搜索 [ ^ ]
MVC4中的模型验证 [ ^ ]
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
"; 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
mvc client validation regex - Google Search[^]
Model Validation in MVC4[^]
这篇关于如何给客户端验证MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!