前台验证:
首先你得有一个参数类,参数类代码如下
[DisplayName("邮箱:")]
[Required(ErrorMessage = "请输入您的邮箱")]
[RegularExpression(@"^(\w-*\.*)[email protected](\w-?)+(\.\w{2,})+$",ErrorMessage ="请输入正确的邮箱")]
public string email { get; set; }
然后再去视图里面利用ValidactionMessageFor验证参数类
<p>
<label id="label_mail" for="mail">邮箱</label>
@Html.TextBoxFor(model => model.email, new { @class = "user_input", tabindex = "4" })
@Html.ValidationMessageFor(model => model.email)
</p>
后台验证:个人觉得还是写一个验证类比较合适,
验证类:
public bool Isemail(string isemail) {
Regex RegCHZN = new Regex(@"^(\w-*\.*)[email protected](\w-?)+(\.\w{2,})+$");
Match m = RegCHZN.Match(isemail);
return m.Success;
}
在控制器里面调用判断是否验证成功:
private bool Validate(Field field)
{
bool isemail=validate.Isemail(field.email);
if(isemail==true)
{
return true;
}
else{
return false;
}
}
if (ModelState.IsValid)
{
//用来判断是否通过前端验证
if(){//这里用来写后端验证
}
}
纯手打,转载请出示原文链接