#Why is validation for password failing?
32 messages · Page 1 of 1 (latest)
<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]],
});
}
password: ['', [
Validators.required,
Validators.minLength(8),
Validators.pattern('(?=.[a-z])'),
Validators.pattern('(?=.[A-Z])'),
Validators.pattern('(?=.*[0-9])')
]],
fails too
What are you trying to do?
Trying to set password validation that has minimum 8 characters, an uppercase and lowercase letter and a number.
Validator will only return a export declare type ValidationErrors are you trying to display an error ?
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;
}
This is okay, but is there a specific reason why the regex is not working?
Per regex rules, "Test12345" should pass validation
(?=.*\\d) needs to be (?=.*\d), don't trust AI
Also, go with this answer #1061822629448798299 message best to avoid regex when you can
unfortunately this doesn't work either
try to add gm after the last / in the regex
nope
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.
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
regeEx come in many flavours.
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.
I just noticed that 15 seconds ago haha
doesn't seem to want to work if it's passed as a string
thanks!