指定必须为属性提供值。
![](http://silverlightchina.net/uploads/allimg/110726/003954AZ-0.jpg)
代码:
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
指定一个实体成员允许的最大字符数和最小字符数。
![](http://silverlightchina.net/uploads/allimg/110726/0039545230-1.jpg)
代码:
privatestring _password;
[StringLength(6, ErrorMessage="密码不能超过6个字符")]
publicstring password
{
get { return _password; }
set
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName ="password" });
_password =value;
}
}
三:最大值或最小值验证RangeAttribute
为关联成员指定最小值和最大值约束。
![](http://silverlightchina.net/uploads/allimg/110726/0039543V6-2.jpg)
代码:
[Range(1, int.MaxValue, ErrorMessage ="请输入大于等于1的数")]
publicint Dept_CodeLeve
{
set
{
if (_dept_codeleve != value)
{
Validator.ValidateProperty(value,
new ValidationContext(this, null, null) { MemberName ="Dept_CodeLeve" });
_dept_codeleve = value;
}
}
get { return_dept_codeleve; }
}
四:电话号码验证RegularExpressionAttribute
指定用于验证关联成员的正则表达式。
![](http://silverlightchina.net/uploads/allimg/110726/0039543255-3.jpg)
代码:
[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,
new ValidationContext(this, null, null) { MemberName ="Dept_Phone" });
_dept_phone = value;
}
}
get { return_dept_phone; }
}
五:电子邮箱地址验证RegularExpressionAttribute
指定用于验证关联成员的正则表达式。
![](http://silverlightchina.net/uploads/allimg/110726/0039542163-4.jpg)
代码:
[RegularExpression(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$", ErrorMessage ="请输入正确的Email格式\n示例:abc@123.com")]
publicstring Dept_Email
{
set
{
if (_dept_email != value)
{
Validator.ValidateProperty(value,
new ValidationContext(this, null, null) { MemberName ="Dept_Email" });
_dept_email = value;
Notify(() => Dept_Email);
}
}
get { return_dept_email; }
}
六:网站地址验证RegularExpressionAttribute
指定用于验证关联成员的正则表达式。
![](http://silverlightchina.net/uploads/allimg/110726/003954J07-5.jpg)
代码:
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, new ValidationContext(this, null, null) { MemberName ="Client_Httpaddr" });
}
}
get { return client_HttpAddr; }
}