<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Conten("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
[Required] [Display(Name = "用户名")] public string UserName { get; set; }
[Required] [StringLength(100, MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; }
[Required] [DataType(DataType.EmailAddress)] [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9._]+\.[A-Za-z]{2,4}")] [Display(Name = "电子邮件地址")] public string Email { get; set; }
[Required] [Range(13, 18)] [Display(Name="年龄")] public int Age { get; set; }
[Required] [Display(Name = "用户名")] [Remote("CheckUserName","Account")] public string UserName { get; set; }
public JsonResult CheckUserName(string userName) { var result = userName == "admin"; return Json(result, JsonRequestBehavior.AllowGet); }
[Required(ErrorMessageResourceType=typeof(ErrorMessage),ErrorMessageResourceName="UserRequire")] [Display(Name = "用户名")] [Remote("CheckUserName","Account", HttpMethod="POST")] public string UserName { get; set; }
而Controller中也可以直接接受Post的请求,当然你加上[Httppost]也是可以的:
//[HttpPost] public JsonResult CheckUserName(string userName) { var result = userName != "admin"; return Json(result, JsonRequestBehavior.AllowGet); }
[Required] [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "确认密码")] [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")] public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "用户名不能为空!")] [Display(Name = "用户名")] [Remote("CheckUserName","Account")] public string UserName { get; set; }
[Required(ErrorMessage = "{0}不能为空!")] [Display(Name = "用户名")] [Remote("CheckUserName","Account")] public string UserName { get; set; }
[Required(ErrorMessage = "{0}不能为空!")] //[Display(Name = "用户名")] [Remote("CheckUserName","Account")] public string UserName { get; set; }
[Required] [StringLength(100, ErrorMessage = "请输入{2}到{1}位的{0}。", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; }
[Required(ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "UserNameRequire")] [Display(Name = "用户名")] public string UserName { get; set; }
public class MyMaxLengthAttribute : ValidationAttribute { private readonly int MaxLength; public MyMaxLengthAttribute(int maxLength) { MaxLength = maxLength; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { string content = value.ToString(); if (content.Length > MaxLength) { return new ValidationResult("输入的字符太多了!^_^"); } return ValidationResult.Success; //return base.IsValid(value, validationContext); }
[Required(ErrorMessageResourceType=typeof(ErrorMessage),ErrorMessageResourceName="UserRequire")]
[Display(Name = "用户名")]
[MyMaxLengthAttribute(10)]
[Remote("CheckUserName","Account", HttpMethod="POST")]
public string UserName { get; set; }
[Required(ErrorMessageResourceType=typeof(ErrorMessage),ErrorMessageResourceName="UserRequire")]
[Display(Name = "用户名")]
[MyMaxLengthAttribute(10,ErrorMessage="{0}字数太多")]
[Remote("CheckUserName","Account", HttpMethod="POST")]
public string UserName { get; set; }
public class MyMaxLengthAttribute : ValidationAttribute { private readonly int MaxLength; public MyMaxLengthAttribute(int maxLength ):base("{0}的字符太多了!") { MaxLength = maxLength; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { string content = value.ToString(); if (content.Length > MaxLength) { //return new ValidationResult("输入的字符太多了!^_^"); string errorMessage = FormatErrorMessage(validationContext.DisplayName); return new ValidationResult(errorMessage); } return ValidationResult.Success; //return base.IsValid(value, validationContext); } }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContent) { if (Password != ConfirmPassword) { yield return new ValidationResult("两次输入的密码不同!", new[] { "Password" }); } }
======================== 四、Remote实现多重验证==========================
public JsonResult CheckUserName(string userName) { if (IsUniqueName(userName) && IsForbiddenName(userName)) { return Json(true, JsonRequestBehavior.AllowGet); } else if (!IsUniqueName(userName)) { return Json("用户名不唯一!", JsonRequestBehavior.AllowGet); } else { return Json("用户名不包含违禁词!", JsonRequestBehavior.AllowGet); } }