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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With