Alea M
Alea M

Reputation: 53

asp.net RegularExpressionValidator and complex regular expression (case sensitive switch)

I'm using ASP's RegularExpressionValidator with a very complex regular expression. This one works already well:

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

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

For example the user name is Meier the user shall not be 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 as ValidationExpression.

Unfortunately, when doing so, I get an error in my browser when adding a password:

"SyntaxError: invalid quantifier"

The problematic code the browser shows is:

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]);
}

The line with the "**" is the problematic one.

I also tried already RegEx.IsMatch, but there my regular expression works. Does anyone have an idea how I can solve this or can anyone tell me what I'm doing wrong?

Any help is very welcome! :) And please do not hesitate to ask if I described anything not well enough.

Upvotes: 3

Views: 1385

Answers (1)

MikeM
MikeM

Reputation: 13631

Unless you set EnableClientScript="False" to disable client-side validation, see msdn, your expression will need to be valid for Javascript's regular expression engine.

That is why var rx = new RegExp(val.validationexpression); is causing an error, your expression is invalid.

Javascript does not support the inline case-sensitivity flags (?i) and (?-i) and your long character class needs revision

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

And as you can't turn on case-insensitivity just for the password negative look-ahead in Javascript, it would be better to test for the password separately.

Upvotes: 4

Related Questions