Search code examples
javascriptangularvalidationangular4-forms

how to check if 2 fields are equal in angular 4


i am developing a application in angular 4. there i have a small form. validations are working fine. but i want to verify that both passwords and the password confirm fields are same.

    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("", []),  


  });

is there a way in angular for form control that i can verify both password and passwordc fields are equal.


Solution

  • You should implement your custom validator directive. I assume you did similar job on "this.valdaitionService.emailDomainValidator).

    For your reference please check the link below:

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