0

i have a problem for validate alphanumeric in mobile browser.

i have code like this.

var regex_alphanumeric = /[^[a-z 0-9]]*/gi;

$('#address').keypress(function (e) {
  if(e.key.match(regex_alphanumeric)){
    e.preventDefault();
    return false;
  }
});

with this code, only support for desktop browser, but if i check in mobile browser, validate alpha numeric not running well because in mobilebrowser e.key value always 229

how to check alphanumeric in mobilebrowser?

teguh11
  • 63
  • 9
  • 1
    Please see [Allowing only Alphanumeric values](https://stackoverflow.com/questions/13236651/allowing-only-alphanumeric-values) probably you can find your answer. – Shubham Baranwal Oct 14 '19 at 07:09
  • Or: event.key is undefined in mobile browsers for keyup, keydown and keypress https://stackoverflow.com/a/45436329/6352710 – ahwayakchih Oct 14 '19 at 07:10
  • @ShubhamBaranwal. it's only work on desktop browser. but in mobile browser charCode always return 229. – teguh11 Oct 14 '19 at 07:35

1 Answers1

0

As mentioned in jquery docs

Note: as the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.

Try this one.

One more solution to take a previous char.

user2809176
  • 1,042
  • 12
  • 29
  • thanks for helping, but in android or iphone, keydown or keyup can't work for validate alphanumeric. because "keycode" always 229, and "key" always undefined. i need validate alphanumeric on android / iphone browser with realtime validate – teguh11 Oct 15 '19 at 03:03
  • @teguh11, the answer is updated. Please have a look. – user2809176 Oct 15 '19 at 07:11