Allowing only numbers and one decimal

后端 未结 6 1930
醉梦人生
醉梦人生 2020-12-19 17:58

Guys and gals i have this piece of JavaScript code that only allows for numbers and one decimal period. The problem i\'m having is that when i tab over to my textbox contro

6条回答
  •  一个人的身影
    2020-12-19 18:57

    Try this,

    $('input').on('keydown', function (event) {
        return isNumber(event, this);
    });
    function isNumber(evt, element) {
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if ((charCode != 190 || $(element).val().indexOf('.') != -1)  // “.” CHECK DOT, AND ONLY ONE.
                && (charCode != 110 || $(element).val().indexOf('.') != -1)  // “.” CHECK DOT, AND ONLY ONE.
                && ((charCode < 48 && charCode != 8)
                        || (charCode > 57 && charCode < 96)
                        || charCode > 105))
            return false;
        return true;
    }
    

提交回复
热议问题