0

I'd like to validate the password before the form is submit using Regex, Id like to know what whats the equivalent regex for the following PasswordValidator settings:

        UserManager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = false,
            RequireLowercase = true,
            RequireUppercase = true,
        };

im using the AngujarJS pattern validation. Example:

 <input type="password" class="form-control" name="inputPassword" ng-minlength="6" np-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*(_|[^\w])).+$/" ng-model="userData.Password" />
<p class="alert-danger" ng-show="userDetails.inputPassword.$error.pattern">The Password is invalid</p> 

But it simply ignore it.

Thank you very much!

Update 1 with the help of accepted answer i come to the following regex ^(?=.[a-z])(?=.[A-Z])(?=.*(_|[^\w])).+$ and added the length validation as ng-minlength="6" for some reason the pattern validation doesnt work on IE9 where the validation prevent the user save, rendering it totally useless.

Update 2

By suggested by stribizhev, changing the any symbol regex to (?=.*[\W_]) fixed IE9 validation, so it works 100% in IE9, final code is

<input type="password" class="form-control" name="inputPassword" ng-minlength="6" ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).+$/" ng-model="userData.Password" />
<p class="alert-danger" ng-show="userDetails.inputPassword.$error.pattern || userDetails.inputPassword.$error.minlength">The Password is invalid</p> 

thank you all very much

montelof
  • 491
  • 1
  • 6
  • 13
  • Can you show your attempts? – Federico Piazza Aug 31 '15 at 22:31
  • I would check this `^(?=.*[^a-zA-Z\d])(?=.*[a-z])(?=.*[A-Z]).{6}$` not sure if it is correct. –  Aug 31 '15 at 22:42
  • The non letter/digit needs to be better defined. Also, in total, the available/allowed characters should be better defined, like at least non-whitespace, 6 or more: `\S{6,}` –  Aug 31 '15 at 22:48
  • @montelof - Fyi: On your **update 2** - Just to be clear, `(?=.*[\W_])` is _exactly identical_ to `(?=.*(_|[^\w]))` in function. Any speed difference due to the alternation is _not_ noticeable in your context. And in no way affects backtracking whatsoever. As for `fixed IE9 validation`, that's not even relavent, or remotely possible !! –  Sep 01 '15 at 20:01
  • @sln I know its weird, using chrome it works either way, but in IE9 only the second one did it. I dont know if its something related to the pipe maybe. – montelof Sep 02 '15 at 23:08
  • @montelof - `I dont know if its something related to the pipe maybe`. I'm pretty sure `pipes`/alternation was included when the first regular expression engine was designed. That would be a long time ago. Its probably something else at fault. I would be worried about the integrity of an engine that appears to have a problems with an alternation. I would separately test a piped expression both inside and outside a lookahead assertion. If it doesn't work inside a lookahead assertion, the engine integrity is in the _crapper_ !! –  Sep 09 '15 at 16:59

1 Answers1

1
^(?=.*[!@#$&*])(?=.*[a-z])(?=.*[A-Z]).{6,}$

I found that answer on SO which will give you hint how to construct your own regex https://stackoverflow.com/a/5142164/2892208

Community
  • 1
  • 1
jgasiorowski
  • 1,033
  • 10
  • 20