3

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

2 Answers2

4

try this

  this.RegisterForm1.controls["form control name"].updateValueAndValidity();

i think may be help you

1

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>
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215