最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery preventDefault not working on android 4.4 default browser - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited May 15, 2015 at 7:13 Sujata Chanda asked May 15, 2015 at 6:59 Sujata ChandaSujata Chanda 3,3952 gold badges22 silver badges36 bronze badges 11
  • 1 Have you tried using an input type="number" support for it is pretty common now: caniuse.com/#feat=input-number – Rory McCrossan Commented May 15, 2015 at 7:01
  • 1 I cannot use type=number as the validation is for array floating numbers which can receive -/+/,/. also. The number keypad will not allow me to enter this keys. – Sujata Chanda Commented May 15, 2015 at 7:04
  • 1 Instead of e use 'event.preventDefault'. Beacuse in ANdriod e is undefined – Sudharsan S Commented May 15, 2015 at 7:04
  • 2 Have another look at the jQuery documentation. The argument a jQuery handler receives isn't the raw event object. It always has preventDefault and stopPropagation, even if the underlying browser doesn't, and setting returnValue or cancelBubble on it is pointless. – T.J. Crowder Commented May 15, 2015 at 7:04
  • 2 @JqueryKing: Er, no, not if the user is using jQuery. – T.J. Crowder Commented May 15, 2015 at 7:04
 |  Show 6 more comments

4 Answers 4

Reset to default 4 +50

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.

I don't know why you want to prevent a key event, but I think you just want to prevent entering of invalid chars. Let's see how you could resolve your problem without preventDefault:

(function(){ // anon function for scope

    var prevVal = $("#test").val(); // get initial value of input

    $('#test').on("input", function (e) { // input will also handle paste event. it handle every input'
        if(/*your `if` here*/ Math.random() > 0.3){
            this.value = prevVal; // You shall not pass!
        }
        prevVal = this.value; // save current valid value
    }); 

})();

http://jsfiddle.net/rk5wuubo/

I hope, it will solve your problem everywhere and never forget about "paste"!

If you are developing website based on angular you should solve the problem in angular way.

Here the sample how to do this.

The idea is to use NgModelController and FormController.

Please use $formatters of angularjs. This will help you check the viewValue before it is set to modelValue.

Please go through this document: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

发布评论

评论列表(0)

  1. 暂无评论