Dilip Gupta

Dilip Gupta

  • 1.2k
  • 687
  • 26.6k

Alternative for keypress event which will work on mobiles

Jun 28 2018 11:55 PM
I want to validate a Textbox using Angular 5 and it should not accept any characters other than numbers.
I have written following code, Its Working on Pc Browser but its not working on Android Mobile Browsers.
Below is my code ,
  1. <mat-form-field fxFlex="20" class="RightInput">  
  2.                    <input matInput name="Indication1PatientNo" formControlName="Indication1PatientNo" placeholder="Patient No." (keypress)="_OnlyNumeriCkeyPress($event)">  
  3.                  </mat-form-field>  
  1. _OnlyNumeriCkeyPress(event: any) {  
  2.        const pattern = /[0-9\+\-\ ]/;  
  3.        let inputChar = String.fromCharCode(event.charCode);  
  4.   
  5.        if (!pattern.test(inputChar)) {  
  6.            // invalid character, prevent input  
  7.            event.preventDefault();  
  8.        }  
  9.    }