2

嗨,我有一个返回数组对象 passwordPolicy 的 api,其中包含

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.

我知道我们可以使用正则表达式模式验证进行验证,请参见下面的 如何使用 Angular 5 验证器模式验证密码强度

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

我需要验证 newpwdctrlname。

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

3 回答 3

3

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

就像是

  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)

在 Stackblitz 上查看此演示

于 2020-10-20T17:55:42.980 回答
0

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

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

你也可以检查这个:

模式验证 - 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 : ''
    );
  }
}
于 2020-10-20T17:36:02.550 回答
0

试试这个。

在角色更改时更改关联的验证器。

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

于 2020-10-20T17:37:24.863 回答