event.preventDefault() not working on Chrome Android OS. Whereas the same action is working on chrome IOS. I even used event.stopPropagation(), event.stopImmediatePropagation().
HTML:
JavaScript
x
2
1
<input class="otherAmount" type="text"id="pVal" name="pVal" onkeydown="validatePaymentToTwoDecimal(this,event);"/>
2
Java Script:
JavaScript
1
12
12
1
function validatePaymentToTwoDecimal(el, evt) {
2
3
if(!$.isNumeric(evt.key)|| evt.key=="."){
4
evt.preventDefault();
5
evt.stopPropagation();
6
evt.stopImmediatePropagation();
7
return false;
8
} else {
9
return true;
10
}
11
}
12
Advertisement
Answer
Based on an answer for a similar question, you should be able to do this to filter the input:
jQuery solution
JavaScript
1
9
1
$("#pVal").on(
2
"input change paste",
3
function filterNumericAndDecimal(event) {
4
var formControl;
5
6
formControl = $(event.target);
7
formControl.val(formControl.val().replace(/[^0-9.]+/g, ""));
8
});
9
Vanilla JavaScript solution
JavaScript
1
14
14
1
var pInput;
2
3
function filterNumericAndDecimal(event) {
4
var formControl;
5
6
formControl = event.target;
7
formControl.value = formControl.value.replace(/[^0-9.]+/g, ""));
8
}
9
10
pInput = document.getElementById("pVal");
11
["input", "change", "paste"].forEach(function (eventName) {
12
pInput.addEventListener(eventName, filterNumericAndDecimal);
13
});
14
This works by removing digits and decimal point characters using a regular expression.