码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular2 Form] Check password match

时间:2016-10-30 07:33:57      阅读:428      评论:0      收藏:0      [点我收藏+]

标签:tom   eth   validator   input   params   rds   define   creat   keep   

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

标签:tom   eth   validator   input   params   rds   define   creat   keep   

原文地址:http://www.cnblogs.com/Answer1215/p/6012367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!