Angular form validators for passwords, emails, universal constraints, and credit cards with reactive and template-driven form support
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Comprehensive password strength validation including character requirements, repetition limits, allowed character constraints, and password confirmation matching.
Validates that no character repeats more than the specified number of times consecutively.
/**
* Validates character repetition limits in password
* @param repeatCount - Maximum allowed consecutive character repetitions
* @returns ValidatorFn that validates character repetition
*/
static repeatCharacterRegexRule(repeatCount: number): ValidatorFn;Usage Example:
import { PasswordValidators } from "ngx-validators";
import { FormControl } from "@angular/forms";
// Password cannot have more than 3 consecutive identical characters
const control = new FormControl('', PasswordValidators.repeatCharacterRegexRule(3));
// Valid: "password123"
// Invalid: "passssword" (4 consecutive 's' characters)Error Object:
{
repeatCharacterRegexRule: {
repeatCount: number
}
}Validates that password contains only characters from the allowed character set.
/**
* Validates that password contains only allowed characters
* @param allowedChars - Array of allowed characters
* @returns ValidatorFn that validates character allowlist
*/
static allowedCharacterRule(allowedChars: string[]): ValidatorFn;Usage Example:
// Only allow alphanumeric characters and basic symbols
const allowedChars = [
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9','!','@','#','$','%'
];
const control = new FormControl('', PasswordValidators.allowedCharacterRule(allowedChars));Error Object:
{
allowedCharacterRule: {
invalidChars: string[],
allowedChars: string[]
}
}Validates minimum number of alphabetical characters (A-Z, a-z) in the password.
/**
* Validates minimum alphabetical character count
* @param amount - Minimum required alphabetical characters
* @returns ValidatorFn that validates alphabetical character count
*/
static alphabeticalCharacterRule(amount: number): ValidatorFn;Usage Example:
// Require at least 3 alphabetical characters
const control = new FormControl('', PasswordValidators.alphabeticalCharacterRule(3));
// Valid: "pass123!" (4 alphabetical chars)
// Invalid: "12345!" (0 alphabetical chars)Error Object:
{
alphabeticalCharacterRule: {
required: number,
actual: number
}
}Validates minimum number of digit characters (0-9) in the password.
/**
* Validates minimum digit character count
* @param amount - Minimum required digit characters
* @returns ValidatorFn that validates digit character count
*/
static digitCharacterRule(amount: number): ValidatorFn;Usage Example:
// Require at least 2 digit characters
const control = new FormControl('', PasswordValidators.digitCharacterRule(2));
// Valid: "password123" (3 digit chars)
// Invalid: "password!" (0 digit chars)Error Object:
{
digitCharacterRule: {
required: number,
actual: number
}
}Validates minimum number of lowercase characters (a-z) in the password.
/**
* Validates minimum lowercase character count
* @param amount - Minimum required lowercase characters
* @returns ValidatorFn that validates lowercase character count
*/
static lowercaseCharacterRule(amount: number): ValidatorFn;Usage Example:
// Require at least 1 lowercase character
const control = new FormControl('', PasswordValidators.lowercaseCharacterRule(1));
// Valid: "Password123!" (7 lowercase chars)
// Invalid: "PASSWORD123!" (0 lowercase chars)Error Object:
{
lowercaseCharacterRule: {
required: number,
actual: number
}
}Validates minimum number of uppercase characters (A-Z) in the password.
/**
* Validates minimum uppercase character count
* @param amount - Minimum required uppercase characters
* @returns ValidatorFn that validates uppercase character count
*/
static uppercaseCharacterRule(amount: number): ValidatorFn;Usage Example:
// Require at least 1 uppercase character
const control = new FormControl('', PasswordValidators.uppercaseCharacterRule(1));
// Valid: "Password123!" (1 uppercase char)
// Invalid: "password123!" (0 uppercase chars)Error Object:
{
uppercaseCharacterRule: {
required: number,
actual: number
}
}Validates minimum number of special characters (non-alphanumeric) in the password.
/**
* Validates minimum special character count
* @param amount - Minimum required special characters
* @returns ValidatorFn that validates special character count
*/
static specialCharacterRule(amount: number): ValidatorFn;Usage Example:
// Require at least 1 special character
const control = new FormControl('', PasswordValidators.specialCharacterRule(1));
// Valid: "Password123!" (1 special char: !)
// Invalid: "Password123" (0 special chars)Error Object:
{
specialCharacterRule: {
required: number,
actual: number
}
}Validates that two password fields match, typically used for password confirmation scenarios.
/**
* Validates that password and confirmation password match
* @param passwordControlName - Name of password control (default: "newPassword")
* @param confirmPasswordControlName - Name of confirmation control (default: "confirmPassword")
* @returns ValidatorFn that validates password matching at form group level
*/
static mismatchedPasswords(passwordControlName?: string, confirmPasswordControlName?: string): ValidatorFn;Usage Example:
import { FormBuilder, FormGroup } from "@angular/forms";
const form: FormGroup = this.fb.group({
newPassword: [''],
confirmPassword: ['']
}, {
validators: [PasswordValidators.mismatchedPasswords()]
});
// Using custom control names
const customForm: FormGroup = this.fb.group({
password: [''],
passwordConfirm: ['']
}, {
validators: [PasswordValidators.mismatchedPasswords('password', 'passwordConfirm')]
});Error Object:
{
mismatchedPasswords: true
}Note: This validator also adds/removes errors directly on the confirmation control using AbstractControlUtil.addError() and AbstractControlUtil.removeError().
For template-driven forms, use the PasswordValidatorDirective with the [password] attribute:
<input
type="password"
name="password"
[(ngModel)]="user.password"
#password="ngModel"
[password]="true"
[repeatCharacter]="4"
[alphabeticalCharacter]="1"
[digitCharacter]="1"
[lowercaseCharacter]="1"
[uppercaseCharacter]="1">
<div *ngIf="password.errors?.repeatCharacterRegexRule">
Password cannot have more than {{password.errors.repeatCharacterRegexRule.repeatCount}} consecutive identical characters.
</div>
<div *ngIf="password.errors?.digitCharacterRule">
Password must contain at least {{password.errors.digitCharacterRule.required}} digit(s).
</div>