Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.preventDefault() not working for android chrome

Tags:

javascript

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;
        }
}
like image 653
Venkat Kiran Avatar asked Feb 02 '17 20:02

Venkat Kiran


1 Answers

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.

like image 65
Lars Gyrup Brink Nielsen Avatar answered Oct 13 '22 04:10

Lars Gyrup Brink Nielsen