Asp.net RegularExpressionValidator and complex regex (case sensitive switch)

I am using ASP RegularExpressionValidator with very complex regex. This already works well:

(?=^.{10,}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[+#<>;.,:|\\-@*!\"§$%&/()=?`´]).*$

      

But I have to expand it to add a check if the current username is part (case insensitive) of the password.

For example, the username is Meier, the user is not allowed to create a password

i012k34KmeIer567 +

So, I changed the expression to

(?=^.{10,}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[+#<>;.,:|\\-@*!\"§$%&/()=?`´])(?i)(?!.*meier)(?-i).*$

      

and added that to asp:RegularExpressionValidator

how ValidationExpression

.

Unfortunately when doing this I get an error in my browser when I add the password:

"SyntaxError: invalid quantifier"

      

Problem code displayed by browser:

function RegularExpressionValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;
    **var rx = new RegExp(val.validationexpression);**
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}

      

Line c "**"

is problematic.

I also tried it already RegEx.IsMatch

, but my regex works there. Does anyone have an idea how I can solve this, or can anyone tell me what I am doing wrong?

Any help is greatly appreciated! :) And please feel free to ask if I have not described anything enough.

+3


source to share


1 answer


If you don't set EnableClientScript="False"

to disable client side validation, see msdn , your expression must be valid for Javascript regex.

This is why it var rx = new RegExp(val.validationexpression);

throws an error, your expression is invalid.

Javascript doesn't support built-in case sensitivity flags (?i)

and (?-i)

, and your long character class needs revision



 [+#<>;.,:|\\@*!"§$%&/()=?`´-]

      

And since you cannot enable case insensitivity only for negative looking passwords in Javascript, it would be better to check the password separately.

+4


source







All Articles