Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 15x 17x 35x 37x 24x 13x 13x 15x 2x 13x 15x 1x 13x 15x 4x 13x 15x 3x 13x 15x 3x 13x 15x 4x 13x 13x 13x | import {
AbstractControl,
ValidationErrors,
ValidatorFn,
} from '@angular/forms';
export const PasswordValidator: ValidatorFn = (
control: AbstractControl
): ValidationErrors | null => {
const value = control.value;
if (!value) {
return null;
}
let errors: string[] = [];
const hasMinimumLength = value.length >= 8;
if(!hasMinimumLength) {
errors.push($localize `:@@error.password.too_short:too short (min 8 characters)`)
}
const hasMaximumLength = value.length <= 128;
if(!hasMaximumLength) {
errors.push($localize `:@@error.password.too_long:too long (max 128 characters)`)
}
const hasUpperCase = /[A-Z]+/.test(value);
if(!hasUpperCase) {
errors.push($localize `:@@error.password.uppercase:needs at least 1 uppercase character`);
}
const hasLowerCase = /[a-z]+/.test(value);
if(!hasLowerCase) {
errors.push($localize `:@@error.password.lowercase:needs at least 1 lowercase character`);
}
const hasNumeric = /[0-9]+/.test(value);
if(!hasNumeric) {
errors.push($localize `:@@error.password.digit:needs at least 1 digit`);
}
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(value);
if(!hasSpecialChar) {
errors.push($localize `:@@error.password.symbol:needs at least 1 symbol (!@#$%^&*(),.?":{}|<>)`);
}
const valid = hasMinimumLength && hasMaximumLength && hasUpperCase && hasLowerCase && hasNumeric && hasSpecialChar;
const messages = !valid ? errors.join(', ') : '';
return valid ?
null :
{
passwordStrength: {
minimumLengthError: !hasMinimumLength,
maximumLengthError: !hasMaximumLength,
upperCaseError: !hasUpperCase,
lowerCaseError: !hasLowerCase,
numericError: !hasNumeric,
specialCharError: !hasSpecialChar,
message: messages.charAt(0).toUpperCase() + messages.slice(1)
}
}
};
|