I need to validate the strength of a password input form field. The requirements are:
What I have searched and tried so far goes below, the results are inconsistent.
It seems to validate the order of the regex validation.
What I need is to just check if at least one of the char "types" are present.
import { Component } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'signup', templateUrl: './signup.component.html', styleUrls: ['./signup.component.scss'] }) export class SignupComponent { form: FormGroup; constructor() { this.init(); } init() { this.form = this.fb.group({ name: ['', [Validators.required]], email: ['', [Validators.required, Validators.email], password: ['', [ Validators.required, Validators.pattern('((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,30})') ]] }); } }
Password Validators are responsible for determining whether a proposed password is acceptable for use and could include checks like ensuring it meets minimum length requirements, that it has an appropriate range of characters, or that it is not in the history.
I took a stab at this with Angular's built-in pattern validator and was able to come up with the following that checks for:
Special characters
password: [ '', [ Validators.required, Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}') ] ]
I'll add that I'm by no means a regex expert. This is simply what worked for me for a closely related case to the OP. Maybe it will help someone else. Mozilla's documentation helped a lot in figuring this out, specifically the Writing a regular expression pattern section.
I´ve not been able to use correctly the Validator Pattern, so I made a Custom Validator, and validate the password field string with three simple regex.
Anyway, I look forward to use correctly the Angular Validator Pattern.
Custom Validator
// password.validator.ts import { FormControl } from '@angular/forms'; export interface ValidationResult { [key: string]: boolean; } export class PasswordValidator { public static strong(control: FormControl): ValidationResult { let hasNumber = /\d/.test(control.value); let hasUpper = /[A-Z]/.test(control.value); let hasLower = /[a-z]/.test(control.value); // console.log('Num, Upp, Low', hasNumber, hasUpper, hasLower); const valid = hasNumber && hasUpper && hasLower; if (!valid) { // return what´s not valid return { strong: true }; } return null; } }
Replaced the Validator Pattern with my Custom Validator
// signup.component.ts import { Component } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { PasswordValidator } from 'validators/password.validator'; @Component({ selector: 'signup', templateUrl: './signup.component.html', styleUrls: ['./signup.component.scss'] }) export class SignupComponent { form: FormGroup; constructor() { this.init(); } init() { this.form = this.fb.group({ name: ['', [Validators.required]], email: ['', [Validators.required, Validators.email], password: ['', [ Validators.required, PasswordValidator.strong ]] }); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With