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:
<input class="otherAmount" type="text"id="pVal" name="pVal" onkeydown="validatePaymentToTwoDecimal(this,event);"/>
Java Script:
function validatePaymentToTwoDecimal(el, evt) { if(!$.isNumeric(evt.key)|| evt.key=="."){ evt.preventDefault(); evt.stopPropagation(); evt.stopImmediatePropagation(); return false; } else { return true; } }
Advertisement
Answer
Based on an answer for a similar question, you should be able to do this to filter the input:
jQuery solution
$("#pVal").on( "input change paste", function filterNumericAndDecimal(event) { var formControl; formControl = $(event.target); formControl.val(formControl.val().replace(/[^0-9.]+/g, "")); });
Vanilla JavaScript solution
var pInput; function filterNumericAndDecimal(event) { var formControl; formControl = event.target; formControl.value = formControl.value.replace(/[^0-9.]+/g, "")); } pInput = document.getElementById("pVal"); ["input", "change", "paste"].forEach(function (eventName) { pInput.addEventListener(eventName, filterNumericAndDecimal); });
This works by removing digits and decimal point characters using a regular expression.