简体   繁体   中英

Combining regular expression validators into a custom validator (C#)

I have three regular expression validators which targets the same textbox but gives different error messages respectively.

How do I combine them in a custom validator to return different ErrorMessage?

public void PasswordValidate(Object source, ServerValidateEventArgs args)
        {
            Regex PasswordComplexity = new Regex(@"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$");
            Regex ConsecutiveCharCheck = new Regex(@"^(?!.*(?:(.)\1{3,})).*$");
            Regex PasswordLiteralCheck = new Regex(@"^((?!(p|P)(a|A)(s|S)(s|S)(w|W)(o|O)(r|R)(d|D)).)*$");

I think you should keep them as the three separate validators. However, if you want to separate password validation from the remainder of the form's validation, then you might look into the ValidationGroup property for grouping their output.

Edit: Based on comments below, I believe the preferred solution was to set the Display property to Dynamic and keep the multiple RegularExpressionValidator's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM