2

Hi I have an api that returns an array object passwordPolicy that contains

PasswordMinLength: 6
passwordMinLowerCase: 1
passwordMinNumber: 1
passwordMinSymbol: 0
passwordMinUpperCase: 1

where the number can keep changing depending upon the role of a user example it can be

PasswordMinLength: Password min length.
PasswordMinLowerCase: This is currently either 0 or 1  So, if it's returning 1 for example it should have 1 minimum lower case. If it's returning 0 then the check should be ignored for lower case.
PasswordMinUpperCase: Same as above for upper case requirement.
PasswordMinSymbol: Same as above for symbol.
PasswordMinNumber: same as above to determine if a number is required in the password.

I know we can do validation using the regex pattern validation seeing the below How to validate password strength with Angular 5 Validator Pattern

but how to achieve validation DYNAMICALLY when the data keeps changing.

I need to validate the newpwdctrlname.

resetPwdForm: FormGroup = new FormGroup({
  newpwdctrlname: new FormControl('', [Validators.required, Validators.minLength(6)]),
  shownewpwdctrlname: new FormControl('', []),
  rptpwdctrlname: new FormControl('', [Validators.required])
});
Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
Margerine
  • 183
  • 1
  • 4
  • 14

3 Answers3

3

You can generate the pattern dynamically using string interpolation

Something like

  passRequirement = {
    passwordMinLowerCase: 1,
    passwordMinNumber: 1,
    passwordMinSymbol: 2,
    passwordMinUpperCase: 1,
    passwordMinCharacters: 8
  };
  pattern = [
    `(?=([^a-z]*[a-z])\{${this.passRequirement.passwordMinLowerCase},\})`,
    `(?=([^A-Z]*[A-Z])\{${this.passRequirement.passwordMinUpperCase},\})`,
    `(?=([^0-9]*[0-9])\{${this.passRequirement.passwordMinNumber},\})`,
    `(?=(\.\*[\$\@\$\!\%\*\?\&])\{${this.passRequirement.passwordMinSymbol},\})`,
    `[A-Za-z\\d\$\@\$\!\%\*\?\&\.]{${
      this.passRequirement.passwordMinCharacters
    },}`
  ]
    .map(item => item.toString())
    .join("");
  resetPwdForm = this.fb.group({
    newpwdctrlname: ['Passwod1@@5', [Validators.required, Validators.pattern(this.pattern)]],
    shownewpwdctrlname: ['', []],
    rptpwdctrlname: ['', [Validators.required]]
  });
  constructor (private fb: FormBuilder) {}

You can then use this as

Validators.pattern(this.pattern)

See this demo on Stackblitz

Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
0

You should use regex in your validators array

Here's an exemple of how to use regex in Angular:

export class TestComponent implements OnInit {
  private phonePattern = '([0-9]{3})[-]([0-9]{3})[-]([0-9]{4})';
  private emailPattern = '[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}';
  public phone: FormControl = new FormControl(
    '',
    [Validators.required, Validators.pattern(this.phonePattern)]
  );
  public email: FormControl = new FormControl(
    '',
    [Validators.required, Validators.pattern(this.emailPattern)]
  );

  public ngOnInit(): void { }
}

You can also check this:

Pattern Validation - Angular Doc

Angular pattern validation - StackOverflow

EDIT

Here's an example on how you could do it:

export class TestComponent implements OnInit {
  private passwordMinLength: number = 6;
  private passwordMinLowerCase: number = 1;
  private passwordMinNumber: number = 1;
  private passwordMinSymbol: number = 0;
  private passwordMinUpperCase: number = 1;
  private passwordPattern: string;
  
  public password: FormControl = new FormControl(
    '',
    [Validators.required, Validators.pattern(this.passwordPattern)]
  );

  public ngOnInit(): void {
    this.passwordPattern = (
      passwordMinLenght ? REGEXString : '.' +
      passwordMinLowerCase ? REGEXString : '' +
      passwordMinNumber? REGEXString : '' +
      passwordMinSymbol? REGEXString : '' +
      passwordMinUpperCase? REGEXString : ''
    );
  }
}
Leccho
  • 467
  • 5
  • 23
  • PasswordMinLength: 6 passwordMinLowerCase: 1 passwordMinNumber: 1 passwordMinSymbol: 0 passwordMinUpperCase: 1 these property value keeps changing. it can be 1 or 0. Based on that i need to validate. How do i do that? – Margerine Oct 20 '20 at 17:47
  • Just edited my answer to your need you just need to create the regex. – Leccho Oct 20 '20 at 19:20
0

Try this.

Change the associated validator on role changes.

import { CustomValidators } from 'ng2-validation';
    //add or remove validator dynamically

    onRoleChange($role ){
    if(role == 'admin')
   { this.resetPwdForm.get('newpwdctrlname').setValidators([Validators.min(3),Validators.required]);
}
else if(role == 'not admin'){
this.resetPwdForm.get('ewpwdctrlname').setValidators([Validators.required, Validators.pattern('<Pattern>'), CustomValidators.equalTo(password]);
}

    }

create custom validators as required .

Refer - https://dzone.com/articles/how-to-create-custom-validators-in-angular

Ayush v
  • 351
  • 2
  • 9
  • PasswordMinLength: 6 passwordMinLowerCase: 1 passwordMinNumber: 1 passwordMinSymbol: 0 passwordMinUpperCase: 1 these property value keeps changing. it can be 1 or 0. Based on that i need to validate. How do i do that? – Margerine Oct 20 '20 at 17:47
  • use custom validators and predefined validators, and change the validation strategy for a form control at runtime. Example updated – Ayush v Oct 20 '20 at 18:04