How do I get the following to work? It always returns a true condition (i.e. always displays the error even if password 1 & password2 are the same).
<div *ngIf="password2 != password1 && password2.touched" class="error-box">* Passwords must match!</div>
Thanks
entire html:
<ion-content padding>
<form [ngFormModel]="authForm" (ngSubmit)="onSubmit(authForm.value)">
<ion-item [class.error]="!username.valid && username.touched">
<ion-label floating>Username</ion-label>
<ion-input type="text" value="" [(ngFormControl)]="username"></ion-input>
</ion-item>
<div *ngIf="username.hasError('required') && username.touched" class="error-box">* Username is required!</div>
<div *ngIf="username.hasError('minlength') && username.touched" class="error-box">* Minimum username length is 3!</div>
<div *ngIf="username.hasError('maxlength') && username.touched" class="error-box">* Maximum username length is 25!</div>
<div *ngIf="username.hasError('checkFirstCharacterValidator') && username.touched" class="error-box">* Username cant't start with number!</div>
<ion-item [class.error]="!password1.valid && password1.touched">
<ion-label floating>Password</ion-label>
<ion-input type="password" value="" [(ngFormControl)]="password1"></ion-input>
</ion-item>
<div *ngIf="password1.hasError('required') && password1.touched" class="error-box">* Password is required</div>
<div *ngIf="password1.hasError('minlength') && password1.touched" class="error-box">* Minimum password length is 6!</div>
<div *ngIf="password1.hasError('maxlength') && password1.touched" class="error-box">* Maximum password length is 25!</div>
<ion-item [class.error]="!password2.valid && password2.touched">
<ion-label floating>Confirm Password</ion-label>
<ion-input type="password" value="" [(ngFormControl)]="password2"></ion-input>
</ion-item>
<div *ngIf="password2.hasError('required') && password2.touched" class="error-box">* Password is required</div>
<div *ngIf="password2.hasError('minlength') && password2.touched" class="error-box">* Minimum password length is 6!</div>
<div *ngIf="password2.hasError('maxlength') && password2.touched" class="error-box">* Maximum password length is 25!</div>
<div *ngIf="password2 != password1 && password2.touched" class="error-box">* Passwords must match!</div>
<br/><br/>
<button type="submit" primary [disabled]="!authForm.valid" block class="form-button-text">Next</button>
</form>
</ion-content>
ts
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators, AbstractControl} from '@angular/common';
import {Validator} from '../validator/validator';
import {CategoryPage} from '../category/category';
import { EmployeeModel } from '../model/EmployeeModel';
@Component({
templateUrl: 'build/pages/register/register.html',
directives: [FORM_DIRECTIVES]
})
export class RegisterPage {
authForm: ControlGroup;
username: AbstractControl;
password1: AbstractControl;
password2: AbstractControl;
passwordGroup: AbstractControl;
constructor(private nav: NavController, private fb: FormBuilder, private employeeModel: EmployeeModel) {
this.authForm = fb.group({
'username': ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.required, Validators.maxLength(25), Validator.checkFirstCharacterValidator])],
'password1': ['', Validators.compose([Validators.required, Validators.minLength(6), Validators.maxLength(25)])],
'password2': ['', Validators.compose([Validators.required, Validators.minLength(6)])],
});
this.username = this.authForm.controls['username'];
this.password1 = this.authForm.controls['password1'];
this.password2 = this.authForm.controls['password2'];
}