Currently, I am utilizing Angular and the Ionic framework. Here is a snippet of the HTML code that I have written:
<div><ion-input type="text" id="phoneNumber" [(ngModel)]="phoneNumber" (keypress)="phoneNumericCheck($event)" [disabled]="isSendParamDisabled" maxlength="11" placeholder="{{ 'TEXT.INPUT_FIELD_PHONE_NUMBER' | translate }}"></ion-input></div>
<ion-input [(ngModel)]="authNumber" type="text" id="Auth" maxlength="6" (keypress)="authNumericCheck($event)" placeholder="{{ 'TEXT.INPUT_FIELD_AUTH_NUMBER' | translate }}"></ion-input>
I have noticed that I used different functions in the two keypress events, with one working for authNumber but not phoneNumber.
Strangely, when only one keypress event is used, it functions correctly. Why does this happen?
Both event functions contain the same contents:
public phoneNumericCheck(event: any) {
const pattern = /[0-9.,]/;
let value = String.fromCharCode(event.charCode);
let success = pattern.test(value);
if(!success) {
event.preventDefault();
this.alertAboutOnlyNumeric();
}
}
Despite this, there are occasions when this event does not work...the reason behind it remains unknown to me.