Silverlight 数据验证

一:不为空验证RequiredAttribute

  指定必须为属性提供值。

  

 

  代码:

        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, 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, 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,
                                              
new ValidationContext(this, null, 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示例: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

  指定用于验证关联成员的正则表达式。

  

  

 

  代码:

[RegularExpression(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" ,
            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; }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值