I am desperate because i cant get this to work. I have tried so many things but nothing works out of my knowledge.
Could someone help me please to get a "compare password" validation working?
Here is my registation.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl, AbstractControl} from '@angular/forms';
@Component({
selector: 'app-registration',
templateUrl: './registration.component.html',
styleUrls: ['./registration.component.css']
})
export class RegistrationComponent implements OnInit {
constructor(
private formBuilder: FormBuilder
) {
this.createForm();
}
form: FormGroup;
loading = false;
helpblock = true;
createForm() {
this.form = this.formBuilder.group({
email: new FormControl('', {
validators: Validators.compose([
Validators.required,
Validators.minLength(5),
Validators.maxLength(40),
Validators.pattern(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)
]),
updateOn: 'change',
}),
username: new FormControl('', {
validators: Validators.compose([
Validators.required,
Validators.minLength(3),
Validators.maxLength(15),
Validators.pattern(/^[a-zA-Z0-9]+$/)
]),
updateOn: 'change'
}),
password: new FormControl('', {
validators: Validators.compose([
Validators.required,
Validators.minLength(7),
Validators.maxLength(35),
Validators.pattern(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{7,35}$/)
]),
updateOn: 'change'
}),
confirm_password: new FormControl('', {
validators: Validators.compose([
Validators.required,
// this.matchingPasswords('password', 'confirm_password').bind(this)
]),
updateOn: 'change'
}),
compare_pws: Validators.call(this.matchingPasswords('password', 'confirm_password')),
});
}
matchingPasswords(password, confirm_password) {
console.log('0');
return (group: FormGroup) => {
if (group.controls[password].value === group.controls[confirm_password].value) {
console.log('1');
return null;
} else {
console.log('2');
return { 'matchingPasswords': true };
}
};
}
register(form) {
console.log(this.form);
}
ngOnInit() {}
}
Everything works fine. Only comparing passwords doesnt work for me..
Is there a way to solve this problem?