Jquery preventDefault not working on android 4.4 default browser Jquery preventDefault not working on android 4.4 default browser angularjs angularjs

Jquery preventDefault not working on android 4.4 default browser


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.