NET MVC modelデータ検証
7450 ワード
MVCは便利なデータ検証を提供しています.modelに関連する正則などを加えるだけで、フロントで検証スクリプトを生成します.二つのjsファイルを参照する必要があります.
jquery.validate.min.js
jquery.validate.unobtrusive.min.js
Modelデータ検証のまとめ:
jquery.validate.min.js
jquery.validate.unobtrusive.min.js
Modelデータ検証のまとめ:
[Display(Name = " ")]
[Required(AllowEmptyStrings = false, ErrorMessage = " ")]
[Range(100, 20000, ErrorMessage = " 100 2 ")]
[RegularExpression(@"^[1-9]\d*$", ErrorMessage = " ")]
public decimal Amount { get; set; }
/// <summary>
///
/// </summary>
[Display(Name = " ")]
[StringLength(20, MinimumLength = 6, ErrorMessage = "{0} {2} {1} ")]
[Required(AllowEmptyStrings = false, ErrorMessage = " ")]
[DataType(DataType.Password)]
public string TradingPassword { get; set; }
[StringLength(10, MinimumLength = 4, ErrorMessage = "{0} {2} {1} ")]
[Display(Name = " ")]
public virtual string LogPassword { get; set; }
[StringLength(10, ErrorMessage = "{0} {1} ")]
[Display(Name = " ")]
public virtual string RealName { get; set; }
[Display(Name = " ")]
//[Range(typeof(DateTime), "2011-12-31", "1950-1-1", ErrorMessage = "{0} {1} {2}")]
public virtual DateTime Birthday { get; set; }
[RegularExpression(@"\d{17}[\d|X]|\d{15}", ErrorMessage = "{0} ")]
[Display(Name = " ")]
public virtual string IdentityNo { get; set; }
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "{0} ")]
[Display(Name = " ")]
public virtual string Email { get; set; }
[Required(ErrorMessage = " ")]
[RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = " ")]
public string Email { get; set; }