Regex to allow only number and single dot on android device

Viewed 43

I have my angular application and has HTML template below:

 <input inputmode="numeric" id="amount1">
  <input inputmode="decimal" id="amount2">

now for the first field, I want to allow only numbers and for the second field, I want to allow numbers + a single dot on an android device.

In my directive, I have below @HostListner event

@HostListener('keypress', ['$event'])
  handleKeyboardEvent(event) {
    const regex = /^[0-9]*\.?[0-9]*$/;
    const text = event.target.value;  
    if (!text.match(regex)) {
      event.preventDefault();
    }

but this does not work for android devices it also allows other special characters, on the web I have a keyCode-based solution that recognizes key ranges and that is not the right solution I believe because the android devices will have different keycodes.

So, I want a regex-based solution that works both on the web and on the android app as well.

0 Answers
Related