0
# Password Validation
1
2
Comprehensive password strength validation including character requirements, repetition limits, allowed character constraints, and password confirmation matching.
3
4
## Capabilities
5
6
### Character Repetition Rule
7
8
Validates that no character repeats more than the specified number of times consecutively.
9
10
```typescript { .api }
11
/**
12
* Validates character repetition limits in password
13
* @param repeatCount - Maximum allowed consecutive character repetitions
14
* @returns ValidatorFn that validates character repetition
15
*/
16
static repeatCharacterRegexRule(repeatCount: number): ValidatorFn;
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { PasswordValidators } from "ngx-validators";
23
import { FormControl } from "@angular/forms";
24
25
// Password cannot have more than 3 consecutive identical characters
26
const control = new FormControl('', PasswordValidators.repeatCharacterRegexRule(3));
27
28
// Valid: "password123"
29
// Invalid: "passssword" (4 consecutive 's' characters)
30
```
31
32
**Error Object:**
33
```typescript
34
{
35
repeatCharacterRegexRule: {
36
repeatCount: number
37
}
38
}
39
```
40
41
### Allowed Character Rule
42
43
Validates that password contains only characters from the allowed character set.
44
45
```typescript { .api }
46
/**
47
* Validates that password contains only allowed characters
48
* @param allowedChars - Array of allowed characters
49
* @returns ValidatorFn that validates character allowlist
50
*/
51
static allowedCharacterRule(allowedChars: string[]): ValidatorFn;
52
```
53
54
**Usage Example:**
55
56
```typescript
57
// Only allow alphanumeric characters and basic symbols
58
const allowedChars = [
59
'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',
60
'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',
61
'0','1','2','3','4','5','6','7','8','9','!','@','#','$','%'
62
];
63
64
const control = new FormControl('', PasswordValidators.allowedCharacterRule(allowedChars));
65
```
66
67
**Error Object:**
68
```typescript
69
{
70
allowedCharacterRule: {
71
invalidChars: string[],
72
allowedChars: string[]
73
}
74
}
75
```
76
77
### Alphabetical Character Rule
78
79
Validates minimum number of alphabetical characters (A-Z, a-z) in the password.
80
81
```typescript { .api }
82
/**
83
* Validates minimum alphabetical character count
84
* @param amount - Minimum required alphabetical characters
85
* @returns ValidatorFn that validates alphabetical character count
86
*/
87
static alphabeticalCharacterRule(amount: number): ValidatorFn;
88
```
89
90
**Usage Example:**
91
92
```typescript
93
// Require at least 3 alphabetical characters
94
const control = new FormControl('', PasswordValidators.alphabeticalCharacterRule(3));
95
96
// Valid: "pass123!" (4 alphabetical chars)
97
// Invalid: "12345!" (0 alphabetical chars)
98
```
99
100
**Error Object:**
101
```typescript
102
{
103
alphabeticalCharacterRule: {
104
required: number,
105
actual: number
106
}
107
}
108
```
109
110
### Digit Character Rule
111
112
Validates minimum number of digit characters (0-9) in the password.
113
114
```typescript { .api }
115
/**
116
* Validates minimum digit character count
117
* @param amount - Minimum required digit characters
118
* @returns ValidatorFn that validates digit character count
119
*/
120
static digitCharacterRule(amount: number): ValidatorFn;
121
```
122
123
**Usage Example:**
124
125
```typescript
126
// Require at least 2 digit characters
127
const control = new FormControl('', PasswordValidators.digitCharacterRule(2));
128
129
// Valid: "password123" (3 digit chars)
130
// Invalid: "password!" (0 digit chars)
131
```
132
133
**Error Object:**
134
```typescript
135
{
136
digitCharacterRule: {
137
required: number,
138
actual: number
139
}
140
}
141
```
142
143
### Lowercase Character Rule
144
145
Validates minimum number of lowercase characters (a-z) in the password.
146
147
```typescript { .api }
148
/**
149
* Validates minimum lowercase character count
150
* @param amount - Minimum required lowercase characters
151
* @returns ValidatorFn that validates lowercase character count
152
*/
153
static lowercaseCharacterRule(amount: number): ValidatorFn;
154
```
155
156
**Usage Example:**
157
158
```typescript
159
// Require at least 1 lowercase character
160
const control = new FormControl('', PasswordValidators.lowercaseCharacterRule(1));
161
162
// Valid: "Password123!" (7 lowercase chars)
163
// Invalid: "PASSWORD123!" (0 lowercase chars)
164
```
165
166
**Error Object:**
167
```typescript
168
{
169
lowercaseCharacterRule: {
170
required: number,
171
actual: number
172
}
173
}
174
```
175
176
### Uppercase Character Rule
177
178
Validates minimum number of uppercase characters (A-Z) in the password.
179
180
```typescript { .api }
181
/**
182
* Validates minimum uppercase character count
183
* @param amount - Minimum required uppercase characters
184
* @returns ValidatorFn that validates uppercase character count
185
*/
186
static uppercaseCharacterRule(amount: number): ValidatorFn;
187
```
188
189
**Usage Example:**
190
191
```typescript
192
// Require at least 1 uppercase character
193
const control = new FormControl('', PasswordValidators.uppercaseCharacterRule(1));
194
195
// Valid: "Password123!" (1 uppercase char)
196
// Invalid: "password123!" (0 uppercase chars)
197
```
198
199
**Error Object:**
200
```typescript
201
{
202
uppercaseCharacterRule: {
203
required: number,
204
actual: number
205
}
206
}
207
```
208
209
### Special Character Rule
210
211
Validates minimum number of special characters (non-alphanumeric) in the password.
212
213
```typescript { .api }
214
/**
215
* Validates minimum special character count
216
* @param amount - Minimum required special characters
217
* @returns ValidatorFn that validates special character count
218
*/
219
static specialCharacterRule(amount: number): ValidatorFn;
220
```
221
222
**Usage Example:**
223
224
```typescript
225
// Require at least 1 special character
226
const control = new FormControl('', PasswordValidators.specialCharacterRule(1));
227
228
// Valid: "Password123!" (1 special char: !)
229
// Invalid: "Password123" (0 special chars)
230
```
231
232
**Error Object:**
233
```typescript
234
{
235
specialCharacterRule: {
236
required: number,
237
actual: number
238
}
239
}
240
```
241
242
### Password Confirmation Matching
243
244
Validates that two password fields match, typically used for password confirmation scenarios.
245
246
```typescript { .api }
247
/**
248
* Validates that password and confirmation password match
249
* @param passwordControlName - Name of password control (default: "newPassword")
250
* @param confirmPasswordControlName - Name of confirmation control (default: "confirmPassword")
251
* @returns ValidatorFn that validates password matching at form group level
252
*/
253
static mismatchedPasswords(passwordControlName?: string, confirmPasswordControlName?: string): ValidatorFn;
254
```
255
256
**Usage Example:**
257
258
```typescript
259
import { FormBuilder, FormGroup } from "@angular/forms";
260
261
const form: FormGroup = this.fb.group({
262
newPassword: [''],
263
confirmPassword: ['']
264
}, {
265
validators: [PasswordValidators.mismatchedPasswords()]
266
});
267
268
// Using custom control names
269
const customForm: FormGroup = this.fb.group({
270
password: [''],
271
passwordConfirm: ['']
272
}, {
273
validators: [PasswordValidators.mismatchedPasswords('password', 'passwordConfirm')]
274
});
275
```
276
277
**Error Object:**
278
```typescript
279
{
280
mismatchedPasswords: true
281
}
282
```
283
284
**Note:** This validator also adds/removes errors directly on the confirmation control using `AbstractControlUtil.addError()` and `AbstractControlUtil.removeError()`.
285
286
## Template-Driven Forms Support
287
288
For template-driven forms, use the `PasswordValidatorDirective` with the `[password]` attribute:
289
290
```html
291
<input
292
type="password"
293
name="password"
294
[(ngModel)]="user.password"
295
#password="ngModel"
296
[password]="true"
297
[repeatCharacter]="4"
298
[alphabeticalCharacter]="1"
299
[digitCharacter]="1"
300
[lowercaseCharacter]="1"
301
[uppercaseCharacter]="1">
302
303
<div *ngIf="password.errors?.repeatCharacterRegexRule">
304
Password cannot have more than {{password.errors.repeatCharacterRegexRule.repeatCount}} consecutive identical characters.
305
</div>
306
<div *ngIf="password.errors?.digitCharacterRule">
307
Password must contain at least {{password.errors.digitCharacterRule.required}} digit(s).
308
</div>
309
```