angular 8 中的动态密码验证器

Dynamic password validator in angular 8

您好,我有一个 api,returns 一个包含

的数组对象 passwordPolicy
PasswordMinLength: 6
passwordMinLowerCase: 1
passwordMinNumber: 1
passwordMinSymbol: 0
passwordMinUpperCase: 1

数字可以根据用户角色不断变化的示例

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.

我知道我们可以使用正则表达式模式验证来进行验证,如下所示 How to validate password strength with Angular 5 Validator Pattern

但是当数据不断变化时如何实现动态验证。

我需要验证新的 pwdctrlname。

resetPwdForm: FormGroup = new FormGroup({
  newpwdctrlname: new FormControl('', [Validators.required, Validators.minLength(6)]),
  shownewpwdctrlname: new FormControl('', []),
  rptpwdctrlname: new FormControl('', [Validators.required])
});

您应该在验证器数组中使用正则表达式

下面是如何在 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 { }
}

你也可以检查这个:

Pattern Validation - Angular Doc

编辑

这是一个关于如何做到这一点的例子:

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 : ''
    );
  }
}

试试这个。

根据角色更改更改关联的验证器。

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]);
}

    }

根据需要创建自定义验证器。

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

您可以使用字符串插值动态生成模式

类似

  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) {}

然后您可以将其用作

Validators.pattern(this.pattern)

this demo on Stackblitz