且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重复密码验证不起作用

更新时间:2023-01-30 10:54:51

这应该可行

  comparePassword = (control: AbstractControl): {[key: string]: boolean} => {
    // get values
    const password = control.get('password');
    const repassword = control.get('repassword');
    // if no values, validated as true
    if (!password || !repassword) {
      return null;
    }
    // if values match, return null, else nomatch: true
    return password.value === repassword.value ? null : { nomatch: true };
  };

然后在您的表单构建中,设置:

and then in your build of form, set:

{validator: this.comparePassword}

in

this.registerForm = fb.group({
    name: ['', Validators.required],
    email: ['', Validators.compose([Validators.pattern("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"), Validators.required])],
    passwords: this.fb.group({
        password: ['', Validators.compose([Validators.required])],
        repassword: ['', Validators.compose([Validators.required])]
      },{validator: this.comparePassword}) // here
});