1. 程式人生 > >Model驗證簡單易懂

Model驗證簡單易懂

mail pass 不一致 conf user pre 密碼 expr message

public bool UserSex { get; set; }

//定義名字
[Display(Name = "年齡")]
[Range(0, 150, ErrorMessage = "年齡範圍0-150")]
public int UserAge { get; set; }
[Required(ErrorMessage = "郵箱不能為空")]
[EmailAddress(ErrorMessage = "郵箱格式不正確")]
//[RegularExpression("^\\w@\\w.\\w$")]
public string UserEmail { get; set; }
[Required(ErrorMessage = "電話不能為空")]

//正則驗證
[RegularExpression("^1[34578]\\d{9}$", ErrorMessage = "手機格式不正確")]

public string UserPhone { get; set; }
public string UserAddressName { get; set; }
public string UserHobby { get; set; }

[Required(ErrorMessage = "密碼不能為空")]
public string UserPass { get; set; }

//密碼驗證
[Compare("UserPass", ErrorMessage = "兩次密碼輸入不一致")]
public string ConfirmUserPass { get; set; }

public int AddressID { get; set; }

Model驗證簡單易懂