指定必须为属性提供值。
代码:
privatestring name;
[required(ErrorMessage ="姓名不能为空")]
publicstring Name
{
get { return name; }
set
{
if (name != value)
{
Validator.ValidateProperty(value,
new ValidationContext(this,null, null) { MemberName ="Name" });
name =value;
}
}
}
二:字符串长度验证StringLengthAttribute
指定一个实体成员允许的最大字符数和最小字符数。
代码:
privatestring _password;
[StringLength(6,ErrorMessage="密码不能超过6个字符")]
publicstring password
{
get { return _password; }
set
{
Validator.ValidateProperty(value,new ValidationContext(this, null) { MemberName ="password" });
_password =value;
}
}
三:最大值或最小值验证RangeAttribute
为关联成员指定最小值和最大值约束。
代码:
[Range(1,int.MaxValue,ErrorMessage ="请输入大于等于1的数")]
publicint Dept_CodeLeve
{
set
{
if (_dept_codeleve != value)
{
Validator.ValidateProperty(value,
new ValidationContext(this, null) { MemberName ="Dept_CodeLeve" });
_dept_codeleve = value;
}
}
get { return_dept_codeleve; }
}
四:电话号码验证RegularExpressionAttribute
指定用于验证关联成员的正则表达式。
代码:
[RegularExpression(@"^((0\d{2,5}-)|\(0\d{2,5}\))?\d{7,8}(-\d{3,4})?$",ErrorMessage ="电话格式有误。\n 有效格式为:\n①本区7或8位号码[-3或4位分机号码,可选]\n②(3~5位区号)7或8位号码[-3或4位分机号码,可选]\n③3~5位区号-7或8位号码[-3或4位分机号码,可选]\n示例:023-12345678;(023)1234567-1234")]
publicstring Dept_Phone
{
set
{
if (_dept_phone != value)
{
Validator.ValidateProperty(value, null) { MemberName ="Dept_Phone" });
_dept_phone = value;
}
}
get { return_dept_phone; }
}
五:电子邮箱地址验证RegularExpressionAttribute
指定用于验证关联成员的正则表达式。
代码:
[RegularExpression(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$",ErrorMessage ="请输入正确的Email格式\n示例:[email protected]")]
publicstring Dept_Email
{
set
{
if (_dept_email != value)
{
Validator.ValidateProperty(value,
new ValidationContext(this, null) { MemberName ="Dept_Email" });
_dept_email = value;
Notify(() => Dept_Email);
}
}
get { return_dept_email; }
}
六:网站地址验证RegularExpressionAttribute
指定用于验证关联成员的正则表达式。
代码:
ErrorMessage ="请输入合法的网址!\n示例:https://abc.a;http://www.abc.dd" )]
publicstring Client_Httpaddr
{
set
{
if (client_HttpAddr != value)
{
client_HttpAddr = value;
Notify(() => Client_Httpaddr);
Validator.ValidateProperty(value, null) { MemberName ="Client_Httpaddr" });
}
}
get { return client_HttpAddr; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。