angular - Keydown event not working in Android Device -
i write function using keydown event let input allow number, '-', , backspace key. when test in ionic serve or ios, worked. when test in android device, not working, input field allow character inputed. when use console.log (event), returns key=“unidentified”, keycode = 229.
environment: ionic 3, node v6.11, npm 3.10, android 5.1.1
i don't know how fix this.
.ts file
import {component} '@angular/core'; import {navcontroller} '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
<ion-header> <ion-navbar> <ion-title>home</ion-title> </ion-navbar> </ion-header> <ion-content> <ion-item> <ion-input type="text" (keydown)="isvalidnumber($event)"></ion-input> </ion-item> </ion-content>
try 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 // more checking arrows, delete, backspace, etc. || e.keycode == 229)) { // check 'placeholder keycode' return false; } { // not valid key e.preventdefault(); } return true; }
found here
please read following: http://lists.w3.org/archives/public/www-dom/2010julsep/att-0182/keycode-spec.html
Comments
Post a Comment