zoukankan      html  css  js  c++  java
  • [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"
        }
      };
    }
  • 相关阅读:
    PHP 高精度计算
    PHPWord使用方法
    羽毛球
    大数据(2)
    大数据(1)
    Centos 7 启动错误:XFS_WANT_CORRUPTED_GOTO 修复
    selenium 自动化工具
    python 开发技巧(0)-- 各个系统的python安装
    Yii简单使用阿里云短信教程!
    VMware虚拟机 Ubuntu 实用技巧 (2)桥接模式连接网络与网卡的配置
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6012367.html
Copyright © 2011-2022 走看看