Asp.Net Mvc表单验证方法

   本文所讲的是在Asp.Net MVC框架下所提供的表单验证方法,实现步骤:1.定义验证规则  2.应用验证规则  3.显示验证信息

   验证规则:1.Required:必填验证 2Compare:比较验证(验证两个值是否一致)3.StringLength:字符串长度验证(可以单独设置最大值,也可以同时设置最小值)4.Range:用于设置数字、时间的范围 5.RegularExpression:正则表达式

   在Model中引用System.ComponentModel.DataAnnotations,并在实体类中添加模型验证特性如下

public class Customer
{
[DisplayName("账号")]
[Required(ErrorMessage = "{0}不得为空")]
public string LoginId { get; set; }
[DisplayName("密码")]
[Required(ErrorMessage = "{0}不得为空")]
[StringLength(18, MinimumLength = 6, ErrorMessage = "{0}的长度在{2}和{1}之间")]
public string LoginPwd { get; set; }
[DisplayName("重复密码")]
[Required(ErrorMessage ="{0}不得为空")]
[Compare("LoginPwd",ErrorMessage ="两次密码输入不一致")]
public string PasswordConfirm { get; set; }
[DisplayName("姓名")]
[Required(ErrorMessage ="{0}不得为空")]
public string Name { get; set; }
[DisplayName("年龄")]
[Required(ErrorMessage ="{0}不得为空")]
[Range(18,60,ErrorMessage ="{0}必须在{1}和{2}之间")]
public int? Age { get; set; }
[DisplayName("地址")]
[Required(ErrorMessage = "{0}不得为空")]
public string Address { get; set; }
[DisplayName("电话")]
[Required(ErrorMessage = "{0}不得为空")]
[StringLength(11,MinimumLength =7,ErrorMessage ="{0}必须在{2}和{1}之间")]
public string Phone { get; set; }
[DisplayName("邮箱")]
[Required(ErrorMessage = "{0}不得为空")]
[RegularExpression(@"^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+
$",ErrorMessage ="请输入正确的{0}")]
public string Email { get; set; }
}

1、添加一个强类型视图,使页面中的实体类型能够制动映射到动作方法中

2、使用ModelState的IsValid属性验证是否所有的值都输入正确

3、使用ModelState的AddModelError("key","values")方法添加自定义错误信息

public ActionResult Register(Customer cu)
{
if(ModelState.IsValid)
{
CustomerManager customer = new CustomerManager();
if(customer.Register(cu)==false)
{
ModelState.AddModelError("doubleUser", "该用户已经存在");
return View("Index",cu);
}
else
{
return Content("<script>alert('注册成功');window.location='" + Url.Content("~/") + "'</script>");
}
}
else
{
return View("Index",cu);//如果验证失败则停留在注册页面
}

}

4、使用@Html.ValidationMessage("Name")显示对应属性的错误信息,也可以使用Html.ValidationAummary()集中显示所有信息,在每个信息框中Values属性中填写value="@(Model!=null?Model.LoginId:"")"用于每次提交信息时将上一次提交的信息依然保存在框中(注意在动作方法中使用return View("Index",cu);其中cu是用户提交到动作方法中的信息,如此才能将用户提交的信息保存)

<form action="/Home/Register" method="post">
<p>用户名:<input name="LoginId" type="text" value="@(Model!=null?Model.LoginId:"")" />@Html.ValidationMessage("LoginId")@Html.ValidationMessage("doubleUser")</p>
<p>密码:<input name="LoginPwd" type="password" value="@(Model!=null?Model.LoginPwd:"")" />@Html.ValidationMessage("LoginPwd")</p>
<p>确认密码:<input name="PasswordConfirm" type="password" value="@(Model!=null?Model.PasswordConfirm:"")" />@Html.ValidationMessage("PasswordConfirm")</p>
<p>姓名:<input name="Name" type="text" value="@(Model!=null?Model.Name:"")" />@Html.ValidationMessage("Name")</p>
<p>年龄:<input name="Age" type="text" value="@(Model!=null?Model.Age:null)" />@Html.ValidationMessage("Age")</p>
<p>地址:<input name="Address" type="text" value="@(Model!=null?Model.Address:"")" />@Html.ValidationMessage("Address")</p>
<p>电话:<input name="Phone" type="text" value="@(Model!=null?Model.Phone:"")"/>@Html.ValidationMessage("Phone")</p>
<p>邮箱:<input name="Email" type="text" value="@(Model!=null?Model.Email:"")" />@Html.ValidationMessage("Email")</p>
<input type="submit" value="注册" />
</form>

原文地址:https://www.cnblogs.com/zjifafengfang/p/12198309.html