Created
November 26, 2019 06:03
-
-
Save ngnam/edb23466771310b8fa2bc4cad42326fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// password.validator.ts | |
import { FormControl } from '@angular/forms'; | |
export interface ValidationResult { | |
[key: string]: boolean; | |
} | |
export class PasswordValidator { | |
public static strong(control: FormControl): ValidationResult { | |
const hasNumber = /(?=\D*\d)/.test(control.value); | |
const hasUpper = /(?=[^A-Z]*[A-Z])/.test(control.value); | |
const hasLower = /(?=[^a-z]*[a-z])/.test(control.value); | |
const minLength = /.{8,20}/.test(control.value); | |
// console.log('Num, Upp, Low', hasNumber, hasUpper, hasLower); | |
const valid = hasNumber && hasUpper && hasLower && minLength; | |
if (!valid) { | |
// return what´s not valid | |
return { strong: true }; | |
} | |
return null; | |
} | |
public static strongHasNumber(control: FormControl): ValidationResult { | |
const hasNumber = /(?=\D*\d)/.test(control.value); | |
const valid = hasNumber; | |
if (!valid) { | |
// return what´s not valid | |
return { strongHasNumber: true }; | |
} | |
return null; | |
} | |
public static strongHasUpper(control: FormControl): ValidationResult { | |
const hasUpper = /(?=[^A-Z]*[A-Z])/.test(control.value); | |
// console.log('Num, Upp, Low', hasNumber, hasUpper, hasLower); | |
const valid = hasUpper; | |
if (!valid) { | |
// return what´s not valid | |
return { strongHasUpper: true }; | |
} | |
return null; | |
} | |
public static strongHasLower(control: FormControl): ValidationResult { | |
const hasLower = /(?=[^a-z]*[a-z])/.test(control.value); | |
// console.log('Num, Upp, Low', hasNumber, hasUpper, hasLower); | |
const valid = hasLower; | |
if (!valid) { | |
// return what´s not valid | |
return { strongHasLower: true }; | |
} | |
return null; | |
} | |
public static strongMinLength(control: FormControl): ValidationResult { | |
const minLength = /.{8,20}$/.test(control.value); | |
// console.log('Num, Upp, Low', hasNumber, hasUpper, hasLower); | |
const valid = minLength; | |
if (!valid) { | |
// return what´s not valid | |
return { strongMinLength: true }; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment