Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery preventDefault not working on android 4.4 default browser

I am working with a angular and jquery based website. I have a text input field for validating array of floating numbers. My requirement is to restrict the user from entering alphabets, etc.

The problem is I am using e.preventDefault() but its not working in android default browser but working perfectly in android chrome.

I have searched a lot but unable to get any solution.

My sample code :-

 $('#test').keydown(function (e) {
      e.stopPropagation();
      e.preventDefault();
      e.returnValue = false;
      e.cancelBubble = true;
      return false;
});

I have also tried :-

$('#test').keydown(function (e) {
  if (e.preventDefault) {
       e.preventDefault();
    } else {
       e.returnValue = false;
  }
});

Working fiddle

Note:- I cannot use keypress event as the android default browser cannot listen this event.

like image 994
Sujata Chanda Avatar asked May 15 '15 06:05

Sujata Chanda


1 Answers

I also had this problem with default browser on Android. preventDefault didn't help me, so I used .on jQuery function.

    $('#test').on('input',function () {
        var inputText = $(this).val();

        var resultText = inputText.replace(/[^0-9]/g, '');
        $(this).val(resultText);
    });

You can add to regex the other characters you need to be allowed. Hope it helps.

like image 103
Hautelman Claudiu Avatar answered Oct 21 '22 23:10

Hautelman Claudiu