2

I have requirement to avoid numeric input in a text type input field. My application is used only in devices like iPhone and Android.

For which I tried using the below code:

$("#custName-info").keypress(function (event) {
            alert(event.charCode);
            var inputValue = event.charCode;
            if (!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
                event.preventDefault();
            }
        });

JSFIddle

Here, the code works perfectly fine with iPhone but it doesn't work in Android devices. on investigating it, I found below information:

  1. keypress is deprecated and hence should be avoided to be used. But if that is the reason, why is it working with my iPhone or on Chrome device emulator?
  2. People have suggested to use either of the three below substitute event:keyup, keydown, input, change
  3. I further found that the any of these 3 events doesnt work for me in devices because they do not give me charCode to identify the numeric inputs.
  4. There was something on keyCode but then I couldnt understand what is the difference in charCode and keyCode

So my final word on this question is, "how can I can prevent user to input numerics in the text field? Web Application being used in iPhone and Android devices".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
  • Not sure why you couldnt find the jquery loaded, but my fiddle had the jquery loaded through cdn.. Anyways.. I will be highly obliged if you can verify the required functionality in your Android Device whenever you get back to your place.. because problem is with the execution in Android device particularly – OM The Eternity Jun 20 '18 at 11:36
  • I am confused a bit on this question and I would provide an answer that solves the base issue but I'm not sure if that is what you are after so I will ask here. If you are having issues preventing the input of numerics using the code provided, why not just strip the invalid values from the text box using keyup/keydown events? This is very straight forward – ViaTech Jun 20 '18 at 12:19

1 Answers1

3

Does this work for you?

FIDDLE

$("#custName-info").on("input",function(event) {
  var inputValue = this.value;
  console.log(inputValue);
  this.value = this.value.replace(/[0-9]/g,"")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="text" id="custName-info" />

Do visit https://bugs.chromium.org/p/chromium/issues/detail?id=118639#c283


Original answer which did not work:

I substituted the keypress for keydown and charCode for which as recommended in

JavaScript KeyCode vs CharCode

FIDDLE - still works in iOS

$("#custName-info").on("keydown",function(event) {
  var inputValue = event.which;
  console.log(inputValue);
  if (!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
    event.preventDefault();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="text" id="custName-info" />
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks Michel, for the heads-up on this but the thing is `keydown` event does work. But with iPhone it yields the expected results that is "do not allow numerics" but it doesnt work with Android. Because `event.which` for iPhone and Android are yielding different values for keydown of any numeric values, say for example, number 7, iPhone logs for 55 and Android logs for 229. It looks like I have to check the `event.which` for Android devices for All numbers and put the check in code accordingly – OM The Eternity Jun 20 '18 at 11:50
  • I just tested in Chorme on Android. It still allows numerics. But at least I have code I can test now – mplungjan Jun 20 '18 at 11:51
  • You may be SOL: https://bugs.chromium.org/p/chromium/issues/detail?id=118639#c283 – mplungjan Jun 20 '18 at 11:56
  • This chromium bug is more relevant.. I am getting 229 for every number or character i type in – OM The Eternity Jun 20 '18 at 12:01
  • 1
    try if this works for you: https://jsfiddle.net/mplungjan/dh47f29w/show - I updated the answer – mplungjan Jun 20 '18 at 12:10
  • 1
    Excellent.... That works for me... Only thing I see little annoying is whenever I try to put in the number, the keypad switches to character keypad from numerics. Thanks you so much – OM The Eternity Jun 20 '18 at 12:45
  • You are welcome. I do not see the issue in iOS and the colleague with the android is not around now ;) – mplungjan Jun 20 '18 at 12:49