How can I modify the function keydown in javascript to not allow a decimal point?

45 views Asked by At

With this I can write only numbers:

validaHoraNumero: function(iContTabla,iContFila){

    var self = this;

    $("#hora_"+iContTabla+"_"+iContFila+"_id").keydown(function (e) { 
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 || $(this).val().indexOf('.') != -1 || (e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) || (e.keyCode >= 35 && e.keyCode <= 40)) {
            return;
        }
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
},

I need to modify this method to not allow write a decimal point "."

How can I do this? thanks.

1

There are 1 answers

2
Manjar On BEST ANSWER

Just remove the 190, which is '.'

$("#field").keydown(function (e) { 
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||     $(this).val().indexOf('.') != -1 || (e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) || (e.keyCode >= 35 && e.keyCode <= 40)) {
        return;
    }
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
    }
});

https://jsfiddle.net/s3xqtdax/

You can also check this solution:

How to allow only numeric (0-9) in HTML inputbox using jQuery?