iPhone returning same keydown event for (hash and 3) and (Asterisk and 8)

events, iphone, javascript, keycode

Solution

  on('keydown keypress',function (e){});

First, triggers twice on iOS (no clue why), and `bind` fails on FireFox, so just use `$().keypress`.

Your filtering was letting through the right keycodes for the numbers you wanted, but, not catching the characters you needed to catch, at first I had a solution going which used e.orginialEvent.keyIdentifier to go after the misbehaving keys, but this failed dramatically on firefox.

In looking for a solution to that problem I found and modified code from this page about keycodes and charcodes in Firefox.

  $(formSelector).keypress(function (e) {
     var k = e.keyCode || e.charCode;
     var c = e.charCode;
     var isArrow = (c == 0 && k >= 37 && k <= 40);
     var isSpecial = ((k == 8) || (k == 9) || (k == 127)) || isArrow;   // backspace, tab, delete
     var isOK = (k >= 48 && k <= 57) ;  // numbers
     return isOK || isSpecial;
   });

Live Versions:

Good Version, Tested on: iOS, Chrome, Firefox, Tested

keyIdentifier version, Failed on: Firefox

Problem

Am working on phone validation and have a requirement of auto formatting the input with phone no and only allow numeric characters to be added . However when i try to restrict the input using keydown and keypress, IPhone is allowing me to enter # and * . When i checked the keydown value , they both are same with 3 and 8 respectively (keycode 51 and 56). This works perfectly in Android browsers but fails in iPhone. Anyone faced similar issue. ``` $(formSelector + ' input[name^="phone"]').on('keydown keypress',function (e) { // Allow: backspace, delete, tab, escape, and enter if ( e.keyCode == 46 || e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 27 || e.keyCode == 13 || // Allow: Ctrl+A (e.keyCode == 65 && e.ctrlKey === true) || // Allow: home, end, left, right (e.keyCode >= 35 && e.keyCode <= 39) ) { // let it happen, don't do anything return; } else { // Ensure that it is a number and stop the keypress if (e.shiftKey || (e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105 ) ) { e.preventDefault(); } }); ``` I also tried one other method which was suggested in stackoverflow to bind input with 'input propertychange' events and then use pattern matching. but this works correct in IPhone , but fails in Android. ``` $(formSelector + ' input[name^="phone"]').bind('input propertychange', function() { var text = $(this).val(); if (isNaN(text)) { $(this).val(text.replace(/[^\d]/gi, '')); } }); ``` Does anyone have a common solution for this problem ?? Thank you in advance

Original source