Reputation:
I have created angular2 web application .in that i have implemented a custom validator with form builder it will work on submit but i need to trigger validation dynamically in other form control value change please any one give suggestion to achieve this
thank you
Upvotes: 3
Views: 5205
Reputation: 86740
Here is the example of form with custom validation
formInitilization() {
this.loginForm = this._fb.group({
email: ['', [Validators.required ,this.emailValidation]]
});
}
emailValidation(control?:FormControl){
let val=control.value.trim();
let emailp:any=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if(!emailp.test(val))
return { isInvalidEmail:true };
return null;
}
<input type="text" formControlName="email" autofocus='true'>
<span *ngIf='!loginForm.controls["email"].valid && loginForm.controls["email"].dirty' >
Error message
</span>
Upvotes: 1
Reputation:
try this
this.RegisterForm1.controls["form control name"].updateValueAndValidity();
i think may be help you
Upvotes: 4