[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"
    }
  };
}
时间: 2024-10-16 19:17:19

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

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

//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, Validators} from '@angular/forms'; @Component({ selector: 'switch-control', templateUrl: './switch-control.componen

[Angular2 Form] Nested formGroup, and usage of formGroupName

We can nest formGorup: this.reactiveForm = fb.group({ username: [ '', [ Validators.required, Validators.minLength(3) ] ], pwds: fb.group({ pwd: '', rpwd: '' }, {validator: passwordValidator}) }); We make password as an own group. So in html, we need

[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 the forms. These streams allow you handle complex scenarios and asynchronous scenarios with relative ease. This example shows you how to log out the va

[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 object represented by the form in a useful and predictable way. ngModelGroup is often used in combination with fieldset as they mostly represent the same

[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 an object and also check that all the inputs are valid. Angular 2 ‘sngForm allows you to get a reference to that object and validity and use them to d

[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 will match up with each input. This lesson shows how to use *ngFor with radio buttons and covers the quirks of the id property and forattributes as w

[Angular2 Form] Build Select Dropdowns for Angular 2 Forms

Select Dropdowns in Angular 2 a built with select and option elements. You use *ngFor to loop through your values and create options and use ngModel to keep track of the value as it changes. <form #formRef="ngForm"> <select name="l

[Angular2 Form] Model Driven Form Custom Validator

In this tutorial we are going to learn how simple it is to create custom form field driven validators while using Angular 2 model driven forms. These type of validators are really just plain functions that follow a set of conventions. We are going to

[Angular2 Form] Angular 2 Template Driven Form Custom Validator

In this tutorial we are going to learn how we can also implement custom form field validation in Angular 2 template driven forms, by creating our own custom validation directive. We are going to see how to write such directive and how its a best prac