开发者

adding ValidationExpression to customvalidator in asp.net

开发者 https://www.devze.com 2023-02-01 12:12 出处:网络
i have this ValidationExpression=\"(\\d{1,}.{0,2} .{2,})|(.{2,} \\d{1,}.{0,2})\" used in regularexpression validator. I want to use the 开发者_C百科same one in a customvalidator. But customvalidator d

i have this ValidationExpression="(\d{1,}.{0,2} .{2,})|(.{2,} \d{1,}.{0,2})" used in regularexpression validator. I want to use the 开发者_C百科same one in a customvalidator. But customvalidator does not have option validationexpression. how can i add this in code or in tag. Please give me sample.


Use ASP.NET Regex Class.

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {

          if (!Regex.IsMatch(args.Value,@"^(\d{1,}.{0,2} .{2,})|(.{2,} \d{1,}.{0,2})$"))
           {
               args.IsValid = false;
           }
           else
           {
             args.IsValid = true;
         }
}

Note: You will need System.Text.RegularExpressions namespace reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消