#Why is validation for password failing?

32 messages · Page 1 of 1 (latest)

lusty rapids
#

I'm using this for password validation:

password: ['', [Validators.required, Validators.minLength(8), Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])')]],

However, Test12345 as an example fails validation.

#
    <div class="form-floating">
      <input id="password" name="password" type="password" formControlName="password" class="form-control" placeholder="Password" >
      <label for="password">Password</label>
    </div>```
#
  constructor(private webService: ApiService, private fb: FormBuilder,  private router: Router) {
    this.angForm = this.fb.group({
      username: ['', [Validators.required, Validators.minLength(1), Validators.maxLength(15)]],
      password: ['', [Validators.required, Validators.minLength(8), Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])')]],
      email: ['', [Validators.required, Validators.email]],
    });
  }
lusty rapids
#

password: ['', [
Validators.required,
Validators.minLength(8),
Validators.pattern('(?=.[a-z])'),
Validators.pattern('(?=.
[A-Z])'),
Validators.pattern('(?=.*[0-9])')
]],

#

fails too

waxen barn
#

What are you trying to do?

lusty rapids
#

Trying to set password validation that has minimum 8 characters, an uppercase and lowercase letter and a number.

chrome chasm
lusty rapids
#

Yeah, doing that via formControlName.

#

And then ngIf

chrome chasm
#

show how you access

#

and how you display the error

waxen barn
#

You have one problem. You solve it with a regex. Now you have two problems.

function containsAtLeastOneUppercaseOneLowercaseAndOneDigitAndNothingElse(s: string) {
  let uppercaseFound = false;
  let lowercaseFound = false;
  let digitFound = false;
  let otherFound = false;
  for (let i = 0; < s.length; i++) {
    const char = s.charAt(i);
    if (s >= 'a' && s <= 'z') {
      lowercaseFound = true;
    } else if (s >= 'A' && s <= 'Z') {
      uppercaseFound = true;
    } else if (s >= '0' && s <= '9') {
      digitFound;
    } else {
      otherFound = true;
    }
  }
  return uppercaseFound && lowercaseFound && digitFound && !otherFound;
}
lusty rapids
lusty rapids
#

Per regex rules, "Test12345" should pass validation

lyric furnace
lusty rapids
lyric furnace
#

try to add gm after the last / in the regex

lusty rapids
#

nope

boreal comet
#

Really: I second (third) the suggestion about avoiding regEx.
The mess you're looking at is exactly the reason why people don't like them.
It's always a slash, a comma, a parenthesis, a dot... or anything unnoticeable.

lusty rapids
#

yeah, but the same regex is working fine in PHP, so this is really puzzling

#

even if I don't end up using it, I want to figure out why it's actually not working

boreal comet
#

regeEx come in many flavours.

lyric furnace
# lusty rapids

Was so focused on the regex that i didn't realise the quotes messed up the pattern Validators.pattern(/^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])[0-9a-zA-z]{8,}$/), should work.

lusty rapids
#

doesn't seem to want to work if it's passed as a string

#

thanks!