[Angular2 Form] Check password match
Form check password match Angular2
2023-09-14 09:00:52 时间
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" } }; }
相关文章
- layui form表单自定义sm格式
- REQUEST FORM 实例
- [React] Simplify and Convert a Traditional React Form to Formik
- [AngularJS]13. Live preview, Form, ng-model
- jquery.form.js失效问题。
- [Angular2 Form] Style Validation in Angular 2 Forms
- ExtJs 备忘录(1)—— Form表单(一) [ 控件使用 ]
- 如何把 SAP ABAP 系统里一张数据库表的内容,显示在 Adobe PDF Form 里
- 132. SAP UI5 Simple Form 控件的使用方法介绍
- SAP Spartacus User form属性的运行时字段明细
- SAP Spartacus user和org user form两处不同的checkbox风格
- application/json 和 application/x-www-form-urlencoded 有什么区别?