jquery only allow input float number

前端 未结 13 1697
既然无缘
既然无缘 2020-12-01 06:28

i\'m making some input mask that allows only float number. But current problem is I can\'t check if multiple dots entered. Can you check those dots and prevent it for me?

13条回答
  •  既然无缘
    2020-12-01 07:18

    Using JQuery.

    $(document).ready(function()
         {
            //Only number and one dot
            function onlyDecimal(element, decimals)
            {
                $(element).keypress(function(event)
                {
                    num = $(this).val() ;
                    num = isNaN(num) || num === '' || num === null ? 0.00 : num ;
                    if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57))
                    {
                        event.preventDefault();
    
                    }
                    if($(this).val() == parseFloat(num).toFixed(decimals))
                    {
                        event.preventDefault();
                    }
                });
            }
    
             onlyDecimal("#TextBox1", 3) ;
    
    
    
        });
    

提交回复
热议问题