"Model类中可以添加的验证标记:
[Required]
public string FirstName { get; set; }
至多n位:
[StringLength(160)]
public string FirstName { get; set; }
要求至少n位:
[StringLength(160, MinimumLength=3)]
public string FirstName { get; set; }
[RegularExpression(@”[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}”)]
public string Email { get; set; }
[Range(35,44)]
public int Age { get; set; }
小数的情况:
[Range(typeof(decimal), “0.00”, “49.99”)]
public decimal Price { get; set; }
[Remote(“CheckUserName”, “Account”)]
public string UserName { get; set; }
然后在AccountController里指定一个CheckUserName方法:
public JsonResult CheckUserName(stringusername)
{
var result = Membership.FindUsersByName(username).Count == 0;
return Json(result, JsonRequestBehavior.AllowGet);
}
[RegularExpression(@”[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}”)]
public string Email { get; set; }
[Compare(“Email”)]
public string EmailConfirm { get; set; }
正则:
[RegularExpression(@”[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}”,
ErrorMessage=”Email doesn’tlook like a valid email address.”)]
public string Email { get; set; }
普通文本:
[Required(ErrorMessage=”Your last name isrequired”)]
[StringLength(160, ErrorMessage=”Your lastname is too long”)]
public string LastName { get; set; }
占位符:
[Required(ErrorMessage=”Your {0} isrequired.”)]
[StringLength(160, ErrorMessage=”{0} is toolong.”)]
public string LastName { get; set; }
"
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。