password validation in angular
This question could be solved with a combination of these two answers: https://stackoverflow.com/a/43493648/6294072 and https://stackoverflow.com/a/47670892/6294072 So first of all, you would need a custom validator for checking the passwords, that could look like this: checkPasswords(group: FormGroup) { // here we have the 'passwords' group const password = group.get('password').value; const confirmPassword = group.get('confirmPassword').value; return password === confirmPassword ? null : { notSame: true } }
Here is what the above code is Doing:
1. We get the password and confirmPassword values from the FormGroup
2. We check if they are the same
3. If they are the same, we return null, which means the validation passes
4. If they are not the same, we return an object with a key of ‘notSame’ and a value of true.