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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM