Angular2检查重新输入的密码

时间:2017-05-05 06:05:43

标签: javascript typescript angular2-routing angular2-forms

我有angular2形式:

this.registerForm = formBuilder.group({
        'name': ['', Validators.required],
        'email': ['', Validators.compose([Validators.pattern("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"), Validators.required])],
        'password': ['', Validators.compose([Validators.minLength(6), Validators.required])],
        'repassword': ['', Validators.compose([Validators.minLength(6), Validators.required])]
    });

但是我无法弄清楚如何用密码检查repassword并抛出错误信息。

**我接着回答Angular 2 form validating for repeat password,但无法包含ControlGroup。

请帮忙

1 个答案:

答案 0 :(得分:0)

您需要实施自定义验证程序,以便比较passwordrepassword字段。

import { AbstractControl } from '@angular/forms';

function comparePassword(c: AbstractControl) {
    return c.get('password').value === c.get(' repassword').value
}

this.myForm = this.fb.group({
    name: ['', Validators.compose([Validators.required])],
    email: [''],
    passwordgroup: this.fb.group({
        password: ['', Validators.compose([Validators.required])],
        repassword: ['', Validators.compose([Validators.required])]
    },  comparePassword)
});