Strongest password policy
This commit is contained in:
committed by
Jose Alberto Hernandez
parent
9b17a6c4f8
commit
8fdf8fb10c
@@ -0,0 +1,37 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
|
||||
import { Subscription } from 'rxjs';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
|
||||
export class PasswordsUtility {
|
||||
// password regex pattern
|
||||
public static PASSWORD_REGEX = '^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*[#$@$!%*?&])[A-Za-z\d#$@$!%*?&].{8,}$';
|
||||
|
||||
public getPasswordValidators(): ValidatorFn[] {
|
||||
return [Validators.required, Validators.pattern(PasswordsUtility.PASSWORD_REGEX), Validators.maxLength(50), Validators.minLength(8)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm Change Password of Users
|
||||
* @param controlNameToCompare Form Control Name to be compared.
|
||||
*/
|
||||
public confirmPassword(controlNameToCompare: string): ValidatorFn {
|
||||
return (c: AbstractControl): ValidationErrors|null => {
|
||||
if (c.value == null || c.value.length === 0) {
|
||||
return null;
|
||||
}
|
||||
const controlToCompare = c.root.get(controlNameToCompare);
|
||||
if (controlToCompare) {
|
||||
const subscription: Subscription = controlToCompare.valueChanges.subscribe(() => {
|
||||
c.updateValueAndValidity();
|
||||
subscription.unsubscribe();
|
||||
});
|
||||
}
|
||||
return controlToCompare && controlToCompare.value !== c.value ? {'notequal': true} : null;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user