Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

p-inputMask ng-model not clearing for invalid value

I am using inputMask field from PrimeNG

<p-inputMask name="maskValue" [(ngModel)]="attr.attributeValue" mask="aa/99" placeholder="aa/99"></p-inputMask>

{{attr.attributeValue}}

But if I enter a in the textbox and focus out, that textbox is clearing but ng-model attr.attributeValue is still hold value a_/__. How could we clear the ng-model as well ?

like image 942
Thomas Avatar asked Oct 16 '22 18:10

Thomas


1 Answers

By looking to the source of inputMask, you can see that there is a boolean property called filled.

So we can access the value of that property to know if the input is filled or not through ViewChild and by using onBlur event where you can reinitialize your model if it is not filled.

HTML

<p-inputMask #myInputMask name="maskValue" [(ngModel)]="attr.attributeValue" mask="aa/99" placeholder="aa/99" (onBlur)="isInputMaskFilled($event)"></p-inputMask>

TS

@ViewChild('myInputMask') myInputMask: InputMask;

isInputMaskFilled(event) {
  if(!this.myInputMask.filled) {
    this.attr = { attributeValue: '' };
  }
}

See StackBlitz

like image 87
Antikhippe Avatar answered Oct 21 '22 08:10

Antikhippe