-
Notifications
You must be signed in to change notification settings - Fork 28
/
PasswordStrengthValidation.ts
43 lines (37 loc) · 2 KB
/
PasswordStrengthValidation.ts
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
import { PasswordEditor, getRemoteDataAsync, localText, stringFormat } from "@serenity-is/corelib";
import { PasswordStrengthRules } from "../../ServerTypes/Extensions/PasswordStrengthRules";
export async function getPasswordStrengthRules(): Promise<PasswordStrengthRules> {
return await getRemoteDataAsync<PasswordStrengthRules>("PasswordStrengthRules");
}
export function addPasswordStrengthValidation(passwordEditor: PasswordEditor, uniqueName?: string) {
getPasswordStrengthRules().then((passwordStrengthRules) => {
passwordEditor.addValidationRule(() => {
if (passwordEditor.value.length < passwordStrengthRules.MinPasswordLength)
return stringFormat(localText("Validation.MinRequiredPasswordLength"), passwordStrengthRules.MinPasswordLength);
}, uniqueName);
if (passwordStrengthRules.RequireDigit) {
passwordEditor.addValidationRule(() => {
if (!(/[0-9]/.test(passwordEditor.value)))
return stringFormat(localText("Validation.PasswordStrengthRequireDigit"));
}, uniqueName);
}
if (passwordStrengthRules.RequireLowercase) {
passwordEditor.addValidationRule(() => {
if (!(/[a-z\p{Ll}]/u.test(passwordEditor.value)))
return localText("Validation.PasswordStrengthRequireDigit");
}, uniqueName);
}
if (passwordStrengthRules.RequireUppercase) {
passwordEditor.addValidationRule(() => {
if (!(/[A-Z\p{Lu}]/u.test(passwordEditor.value)))
return localText("Validation.PasswordStrengthRequireUppercase");
}, uniqueName);
}
if (passwordStrengthRules.RequireNonAlphanumeric) {
passwordEditor.addValidationRule(() => {
if (!(/[^\s\p{L}]/u.test(passwordEditor.value)))
return localText("Validation.PasswordStrengthRequireNonAlphanumeric");
}, uniqueName);
}
});
}