[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"
    }
  };
}
复制代码

 

posted @   Zhentiw  阅读(571)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示