login - How to add confirm password field in angular 2 form (Tried many examples) -
please me angular 2 form. want add confirm password field can verify password.
here code.
export class signupcomponent implements oninit { signupform: any; result: any; constructor( private formbuilder: formbuilder, private accountservice: accountservice, private router: router ) {} ngoninit() { this.signupform = this.formbuilder.group({ username: ['', [validators.required]], password: ['', [validators.required]], fullname: ['', [validators.required]], email: ['', [validators.required]] }); } save(event: any) { this.accountservice.create(this.signupform.value).subscribe(data => { if(data.count == 0) { this.router.navigate(['/login']); } else { this.result = data } }); }
write custom validation
import {abstractcontrol} '@angular/forms'; export class passwordvalidation { static matchpassword(ac: abstractcontrol) { let password = ac.get('password').value; // value in input tag let confirmpassword = ac.get('confirmpassword').value; // value in input tag if(password != confirmpassword) { console.log('false'); ac.get('confirmpassword').seterrors( {matchpassword: true} ) } else { console.log('true'); return null } } }
in componet add custom validator this
import { passwordvalidation } './password-validation'; this.form = fb.group({ password: ['', validators.required], confirmpassword: ['', validators.required] }, { validator: passwordvalidation.matchpassword // validation method })
wiki
Comments
Post a Comment