Madasu K
Madasu K

Reputation: 1863

Model driven form validation issue in Angular 2

In Angular 2, Model Driven Form is using a Form Builder.

I have:

 ngOnInit() {
    this.user = this.fb.group({
        id: ['', [Validators.required, Validators.minLength(2)]],
        name: ['', [Validators.required, Validators.minLength(5)]]
    });
    
    this.user.get('id').valueChanges.subscribe(value => {
                alert("Change detected); 
                });
}

and in HTML I have :

<input type="text" formControlName="id" >

But what happening is for every letter I change in text box the alert("Change detected"); is getting fired, instead after tabout only I need to call alert("Change detected"). I know in AngularJS to achieve this by using ng-model-options, but how to achieve the same in Angular 2?

Upvotes: 1

Views: 80

Answers (1)

Roman C
Roman C

Reputation: 1

You can bind to onblur handler

<input type="text" formControlName="id" (blur)="onBlur()">

and in class

onBlur(){
  alert("Change detected");  
}

Upvotes: 1

Related Questions