如何检查2个字段在角度4中是否相等

时间:2018-01-23 08:33:21

标签: javascript angular validation angular4-forms

我正在开发一个角度为4的应用程序。我有一个小形式。验证工作正常。但我想验证密码和密码确认字段是否相同。

    registerUser = this.fb.group({
     name: new FormControl(this.data.name, Validators.required),
     email: new FormControl({value: this.data.email, disabled: true}, [ 
            Validators.required,
            Validators.email,
            this.valdaitionService.emailDomainValidator
        ]),
     emailp: new FormControl(this.data.emailp,Validators.email),
     designation:new FormControl({value: this.data.designation, disabled: true}, Validators.required),
     empType:new FormControl({value: this.data.empType, disabled: true}, Validators.required),
     mobileNo:new FormControl(this.data.mobileNo, [Validators.required]), 
     year:new FormControl({value: this.data.year, disabled: true}, [Validators.required]), 
     month:new FormControl({value: this.data.month, disabled: true}, [Validators.required]),    
     password: new FormControl("", []),  
     passwordc: new FormControl("", []),  


  });

有形式控制的角度方式,我可以验证密码和密码c字段是否相等。

2 个答案:

答案 0 :(得分:1)

您应该实现自定义验证程序指令。 我假设你在“this.valdaitionService.emailDomainValidator”上做了类似的工作。

供您参考,请查看以下链接:

https://scotch.io/tutorials/how-to-implement-a-custom-validator-directive-confirm-password-in-angular-2

答案 1 :(得分:0)

registerUser = this.fb.group({
                 name: new FormControl(this.data.name, Validators.required),
                 email: new FormControl({value: this.data.email, disabled: true}, [ 
                        Validators.required,
                        Validators.email,
                        this.valdaitionService.emailDomainValidator
                    ]),
                 emailp: new FormControl(this.data.emailp,Validators.email),
                 designation:new FormControl({value: this.data.designation, disabled: true}, Validators.required),
                 empType:new FormControl({value: this.data.empType, disabled: true}, Validators.required),
                 mobileNo:new FormControl(this.data.mobileNo, [Validators.required]), 
                 year:new FormControl({value: this.data.year, disabled: true}, [Validators.required]), 
                 month:new FormControl({value: this.data.month, disabled: true}, [Validators.required]),    
                 password: new FormControl("", []),  
                 passwordc: new FormControl("", []),  
            {
                  validator: this.MatchConfirom('passwordc','passwordc'),
                }

              });


          private MatchConfirom(type1: any, type2: any) {

            return (checkForm: FormGroup) => {
              let value1 = checkForm.controls[type1];
              let value2 = checkForm.controls[type2];


              if (value1.value === value2.value ) {
                return value2.setErrors(null);
              } else {
                return value2.setErrors({ notEquivalent: true });
              }
            };
          }
Here MatchConfirom(pass1,pass2) is match function which passes two parameter password & confirom password.it calls inside formgroup instance. if both password is correct than setErrors is true for value instance otherwise false.