自定义mvc验证特性,手机号号段老增加,给自定义一个RegularExpress

public class PhoneExpressionAttribute: RegularExpressionAttribute, IClientValidatable
{
public PhoneExpressionAttribute()
: base(@"^1[2,3,4,5,6,7,8,9][0-9]{1}[0-9]{8}$")
{
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentCulture,"手机号码不正确",name);
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var name = metadata.GetDisplayName();
var rule = new ModelClientValidationRegexRule(FormatErrorMessage(name), Pattern);
yield return rule;
}
}

 

 

引用
[PhoneExpression]
public string Mobile { get; set; }

posted on 2018-01-11 11:53 秦少66 阅读(...) 评论(...) 编辑 收藏

导航

公告

统计