angular 2 check validity when no covering ngform

49 Views Asked by At
  <input  matInput  [(ngModel)]="testDate" (ngModelChange)="mychange($event)" [value]="minDate" [min]="minDate" [max]="maxDate" [matDatepicker]...

I dont have a covering ngform or submit button. In myChange() how can I check if testDate is valid. (just like if there was a covering form/control group we can do form.isvalid..to enable/disable the submit button

1

There are 1 best solutions below

3
Jose Anibal Rodriguez On

I think that the best way for you to do that is to put your field inside a form. But you can work around it, and add some validation for the change event, maybe testing if it is null, or if it is an empty string or if it's a valid date object. Then, use a Boolean field and assign to it the result of the validation and use that field on your button.

<button (click)="callMyCallBackFunction($event)" [disabled]="myValidationInputField"></button>