首页 > 代码库 > [Angular2 Form] Check password match
[Angular2 Form] Check password match
Learn how to create a custom validator to check whether passwords match.
<h1>password match</h1><form novalidate autocomplete="off" [formGroup]="signupForm"> <div class="form-field"> <label>Password:</label> <input type="text" formControlName="password" [(ngModel)]="signup.password" name="password"> </div> <div class="form-field"> <label>Confirm Password: </label> <input type="text" formControlName="confirm" [(ngModel)]="signup.confirm" name="confrim"></div></form>
this.signupForm = fb.group({ password: [ ‘‘, Validators.required ], confirm: [ ‘‘, [ Validators.required, confirmPasswords.bind(undefined, this.signup) ] ] });
confirmPasword validator is just a function, also a curry function. So it means it will be invoked when we pass the value ‘confirm‘ password field.
So if we want to send extra params, we can use ‘.bind(undefined, extraParam)‘.
bind to undefined context, will keep the context when it get invoked, then send a extraParam.
The extraParam will be ‘passwords‘ and the value later be passed in is ‘confirm‘.
confirmPassword.ts:
export function confirmPasswords(passwords, confirm) { const valid = passwords.password&& passwords.password === confirm.value; return valid ? null : { confirmPassword: { valid: false, message: "Two passwrods are not the same" } };}
[Angular2 Form] Check password match
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。