JQuery allow only two numbers after decimal point

前端 未结 7 930
春和景丽
春和景丽 2020-12-02 10:31

I found the following JQuery function here which prevents a user from entering anything that\'s not a number or a single decimal. The function works well but I\'d like to i

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 11:08

    Try this

    HTML

    
    

    Jquery

    function isPrice(evt,value) {
        var charCode = (evt.which) ? evt.which : event.keyCode;
        if((value.indexOf('.')!=-1) && (charCode != 45 && (charCode < 48 || charCode > 57)))
            return false;
        else if(charCode != 45 && (charCode != 46 || $(this).val().indexOf('.') != -1) && (charCode < 48 || charCode > 57))
            return false;
        return true;
    }
    

    Worked Link demo

提交回复
热议问题