I am trying to validate password and repeat password in a form. Like this
import { FormGroup, FormBuilder, Validators, AbstractControl } from '@angular/forms';
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])]
}, this.comparePassword)
});
function comparePassword(c: AbstractControl) {
return c.get('password').value === c.get(' repassword').value
}
** I also tried passing return false in comparePassword as:
comparePassword(c: AbstractControl) {
return false;
}
** but every time console.log(this.registerForm.valid); gives me true, but it should return false if i am giving different inputs in password and repeatpassword. So please help how to resolve this. Thanks in advance
A strange thing: custom validation should
return null
when valid.If the validation fails you should return a validation error
{ "comparePassword": true }
So I think your function should be:
This should work
and then in your build of form, set:
in