0

I have a function to validate in input field / date field. When using (keypress) it is working fine on web but not in mobile. And when I am using (keyup) , the validation is not working in web as well.

What is the permanent solution for PWA Angular for this issue ?

// to validate date in numberic and / only
  keyPressAlphanumeric(event: any) {
    var inp = String.fromCharCode(event.keyCode);
    if (/[0-9_/]/.test(inp)) {
      return true;
    } else {
      event.preventDefault();
      return false;
    }
  }


<input matInput [matDatepicker]="picker"  maxlength="10" [max]="maxDate" placeholder="MM/DD/YYYY" required
                formControlName="DOI" autocomplete="off" (keypress)="keyPressAlphanumeric($event)">
Optimist Rohit
  • 328
  • 2
  • 11
  • 1
    According to other stackoverflow questions like [this](https://stackoverflow.com/questions/21055270/javascript-keypress-event-not-raised-on-android-browser) and [this](https://stackoverflow.com/questions/22473950/keypress-event-not-firing-in-android-mobile) it seems to be not possible to use `keypress` on mobile. I'd recommend using `input` event. – Adrian Kokot Jan 07 '22 at 10:50

0 Answers0