swa*_*ath 5 javascript validation angular angular-reactive-forms
我在下面附上了我的代码。我在添加密码不匹配验证时遇到问题。如果我输入不匹配的密码,我不会收到验证错误
注册.component.html
<div class="form-group row mt-3">
<label class="col-md-4 col-lg-4 text-center">UserName:<span style="color:red">*</span></label>
<input kendoTextBox required type="text" class=" col-md-6 col-lg-6 form-control " placeholder="Enter Your UserName " formControlName="username"/>
<div *ngIf="(submitted||f2.username.touched) && f2.username.invalid" class="error-msg">
<div *ngIf="f2.username.errors.required">UserName is required</div>
</div> </div>
<div class="form-group row">
<label class="col-md-4 col-lg-4 text-center">Password:<span style="color:red">*</span></label>
<input kendoTextBox type="password" required class=" col-md-6 col-lg-6 form-control " placeholder="Enter Your passowrd " formControlName="password"/>
<div *ngIf="(submitted||f2.password.touched) && f2.password.invalid" class="error-msg">
<div *ngIf="f2.password.errors.required">password is required</div>
<div *ngIf="f2.password.errors.minlength">minimum of 6 characters required</div>
</div>
</div>
<div class="form-group row">
<label class="col-md-4 col-lg-4 text-center">ConfirmPassword:
<span style="color:red">*</span></label>
<input kendoTextBox required type="password" class=" col-md-6 col-lg-6 form-control " placeholder="Enter Your new password " formControlName="confirmpassword"/>
<div *ngIf="(submitted||f2.confirmpassword.touched) && f2.confirmpassword.invalid" class="error-msg">
<div *ngIf="f2.confirmpassword.errors.required"> confirm password is required</div> <div *ngIf="f2.confirmpassword.errors.minlength">minimum of 6 characters required
</div>
<div class="error-msg" *ngIf="f2.errors?.mismatch && (f2.controls['confirmpassword'].required || f2.controls['confirmpassword'].touched)">
Passwords don't match.
</div>
</div>
</div>
enter code here
Run Code Online (Sandbox Code Playgroud)
registercomponent.ts 文件
在这里我使用了 formBuilder.other 事情工作正常,只有不匹配的验证不起作用
this.registerForm3 = this.formBuilder.group({
username:['', Validators.required],
password: ['', [Validators.required, Validators.minLength(6)]],
confirmpassword:['', [Validators.required, Validators.minLength(6)]],
},
{validator: this.passwordMatchValidator},
);
passwordMatchValidator(frm: FormGroup) {
eturn frm.controls['password'].value ===
frm.controls['confirmpassword'].value ? null : {'mismatch': true};
}
get f2() {
return this.registerForm3.controls;
}
Run Code Online (Sandbox Code Playgroud)
我不认为您可以为自定义验证器使用组件成员函数(方法)。我认为它需要是您班级的外部函数。
我的看起来像这样:
function emailMatcher(c: AbstractControl): { [key: string]: boolean } | null {
const emailControl = c.get('email');
const confirmControl = c.get('confirmEmail');
if (emailControl.pristine || confirmControl.pristine) {
return null;
}
if (emailControl.value === confirmControl.value) {
return null;
}
return { 'match': true };
}
Run Code Online (Sandbox Code Playgroud)
我将验证器附加到子表单组,如下所示:
this.customerForm = this.fb.group({
firstName: ['', [Validators.required, Validators.minLength(3)]],
lastName: ['', [Validators.required, Validators.maxLength(50)]],
emailGroup: this.fb.group({
email: ['', [Validators.required, Validators.email]],
confirmEmail: ['', Validators.required],
}, { validator: emailMatcher }),
phone: ''
});
Run Code Online (Sandbox Code Playgroud)
您可以将验证器放在单独的文件中,或者放在组件类上方或下方的同一文件中。像这样的东西:
function passwordMatchValidator(frm: FormGroup) {
return frm.controls['password'].value ===
frm.controls['confirmpassword'].value ? null : {'mismatch': true};
}
Run Code Online (Sandbox Code Playgroud)
然后你将定义没有this关键字的验证器:
{validator: passwordMatchValidator},
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到完整的例子:https : //github.com/DeborahK/Angular-ReactiveForms/tree/master/Demo-Final
Par*_*ami -9
您可以检查两者的表单值,如下所示。
<div class="error-msg" *ngIf="registerForm3.value.password !== registerForm3.value.confirmpassword">
Passwords don't match.
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20179 次 |
| 最近记录: |