0

In the form password field is there which has a specific pattern(small alphabet, capital alphabet, special, length)

components.ts

Initialize a boolean array for keeping track of pattern which is updated onkeyup:

passwd_pattern_ele: boolean[] = new Array(4);

Formcontrol for password field:

nPassword: ['', [Validators.required, Validators.minLength(8), password_pattern(this.passwd_pattern_ele).bind(this)]]

Custom validator:

This function takes passwd_pattern_ele array as parameter which is updated dynamically

function password_pattern(pass_pat_ele: boolean[]){
  return (control: AbstractControl): { [key: string]: any } | null =>{
    for (let i = 0; i < pass_pat_ele.length; i++) {
      var element = pass_pat_ele[i];
      if(!element){
        return { 'pass_pattern' : pass_pat_ele };
      }
    }
    return null;
  };
}

passwd_pattern_ele which is passed as parameter is not updating in side the function and is empty as when initialized. passwd_pattern_ele is updating outside.

How to get the updated data in passwd_pattern_ele array inside password_pattern()?

2
  • Your code is partial, but it's clearly incorrect. The goal of a validator is to validate the value of the form control that it receives as argument. But you're not doing anything with the received form control. Commented Oct 13, 2019 at 15:06
  • I want to validate text field using custom validator which takes boolean array as parameter, depending on the array values like all true then valid, any one of them is false then invalid. As the data from text box is filled on keyup the array is modified and is working correctly but in the validator function its taking as empty for all 4. Commented Oct 14, 2019 at 5:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.