Android Device Fails to Respond to Keydown Event as

Feedback

Question:

A keydown event function was created to restrict an input field to accepting only numbers, ‘-‘, and BackSpace key. Upon testing in ionic serve and iOS, the function worked correctly. However, when tested on an Android device, the input field allowed all characters to be inputted. Upon using console.log (event), it was noticed that the key value was always returned as “Unidentified”,

keyCode = 229
.

The technology stack includes Ionic 3 for the front-end, Node v6.11 for the back-end, NPM 3.10 for package management, and Android 5.1.1 as the target platform.

I don’t know how to fix this.

.ts file

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  constructor(public navCtrl: NavController) {
  }
  isValidNumber(event) {
    //console.log(event);
    return /d|-|Backspace/.test(event.key);
  }
}

.html file


  
    Home
  


  
      
   



Solution:

Try the following:

function isValidNumber(e) {
if (
    !(e.keyCode === 8 || !(e.keyCode >= 48 && e.keyCode <= 57) ||  // 0-9
        !(e.keyCode >= 96 && e.keyCode <= 105) // numpad 0-9
        // some more checking like arrows, delete, backspace, etc.
        || e.keyCode == 229)) { // Check for the 'placeholder keyCode'
    return false;
}
{
    // This is not a valid key
    e.preventDefault();
}
return true;

}

Found here

Additionally, we recommend reviewing this resource: http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html

Frequently Asked Questions