Learn how to create a custom validator to check whether passwords match.

  1. <h1>password match</h1>
  2. <form novalidate autocomplete="off" [formGroup]="signupForm">
  3. <div class="form-field">
  4. <label>Password:</label>
  5. <input type="text" formControlName="password" [(ngModel)]="signup.password" name="password">
  6. </div>
  7. <div class="form-field">
  8. <label>Confirm Password: </label>
  9. <input type="text" formControlName="confirm" [(ngModel)]="signup.confirm" name="confrim"></div>
  10. </form>
  1. this.signupForm = fb.group({
  2. password: [
  3. '',
  4. Validators.required
  5. ],
  6. confirm: [
  7. '',
  8. [
  9. Validators.required,
  10. confirmPasswords.bind(undefined, this.signup)
  11. ]
  12. ]
  13. });

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:

  1. export function confirmPasswords(passwords, confirm) {
  2. const valid = passwords.password&& passwords.password === confirm.value;
  3. return valid ? null : {
  4. confirmPassword: {
  5. valid: false,
  6. message: "Two passwrods are not the same"
  7. }
  8. };
  9. }

[Angular2 Form] Check password match的更多相关文章

  1. C#实现的Check Password和锁定输错密码锁定账户

    C#实现的Check Password,并根据输错密码的次数分情况锁定账户:如果输入错误3次,登录账户锁定5分钟并提示X点X分后重试登录.如果5分钟后再次输入,累计输入错误密码累计达到5次.则账户会被 ...

  2. ux.form.field.Password 密码与非密码状态切换

    效果如图: 扩展源码: //扩展 //密码按钮扩展 //支持在密码与非密码之间切换 Ext.define('ux.form.field.Password', { extend: 'Ext.form.f ...

  3. [Angular2 Form] Create custom form component using Control Value Accessor

    //switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...

  4. [Angular2 Form] Nested formGroup, and usage of formGroupName

    We can nest formGorup: this.reactiveForm = fb.group({ username: [ '', [ Validators.required, Validat ...

  5. [Angular2 Form] Create Radio Buttons for Angular 2 Forms

    Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels ...

  6. [Angular2 Form] Use RxJS Streams with Angular 2 Forms

    Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...

  7. [Angular2 Form] Understand the Angular 2 States of Inputs: Pristine and Untouched

    Angular 2’s ngModel exposes more than just validity, it even gives you the states of whether the inp ...

  8. [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup

    The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...

  9. [Angular2 Form] Create and Submit an Angular 2 Form using ngForm

    Forms in Angular 2 are essentially wrappers around inputs that group the input values together into ...

随机推荐

  1. ble_app_hrs心率程序 nrf51822

    所用程序为: H:\keil\ARM\Device\Nordic\nrf51822\Board\pca10001\s110\ble_app_hrs 上面的路径是安装sdk之后生成在keil软件所在目录 ...

  2. 【Codeforces Round #457 (Div. 2) B】Jamie and Binary Sequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把n分解成二进制的形式. n=2^a0+2^a1+...+2^a[q-1] 则固定就是长度为q的序列. 要想扩展为长为k的序列. 可 ...

  3. 【习题 8-8 UVA - 1612】Guess

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] double千万不要用==判断相等... 而且两个保留2位有效数字的数字x,y 判断它们相等应该这样. int temp1 = ro ...

  4. BZOJ 1027 JSOI2007 合金 计算几何+Floyd

    题目大意:给定一些合金,选择最少的合金,使这些合金能够按比例合成要求的合金 首先这题的想法特别奇异 看这题干怎么会想到计算几何 并且计算几何又怎么会跟Floyd挂边 好强大 首先因为a+b+c=1 所 ...

  5. makeMtk- user 版本编译

    有时候我们需要在我们的手机上编译user 版本,先说一下user 跟eng版本的区别 user:这个版本是没有root权限的,当你adb shell进入后,linux下显示的是$,不可以push ap ...

  6. php课程 12-41 多态是什么

    php课程 12-41 多态是什么 一.总结 一句话总结:一种请求,多种响应(根据参数类型和数量) 1.function useUsb(Usb $usb){}这句话是什么意思? 判断$usb参数是否实 ...

  7. js ---- 实现千位分隔符

    第一种方法: var num = 1234567; var string = num.toString(); var arr = string.split('').reverse(); console ...

  8. 使用C#对XML进行增删改查操作

    xml文件格式 <?xml version="1.0" encoding="utf-8"?> <messageList> <mes ...

  9. GeoServer 常见问题总结 (转)

    geoserver在部署发布服务时,经常会遇到如下问题,现总结如下: 1.忘记了GeoServer Web Admin Page的登陆用户名和密码怎么办?存储位置:C:\Program Files\G ...

  10. Android学习笔记之Bitmap位图的缩放

    位图的缩放也可以借助Matrix或者Canvas来实现. 通过postScale(0.5f, 0.3f)方法设置旋转角度,然后用createBitmap方法创建一个经过缩放处理的Bitmap对象,最后 ...