The input box can only enter an array, or only a positive integer with 2 decimals

Only positive integers can be entered

input code

<input id="stock" name="stock" class="form-control" type="text" oninput="justInteger(this)">

js code:

    //只能输入正整数
    function justInteger(obj){
        obj.value=obj.value.replace(/^(0+)|[^\d]+/g,'')
    }

Only two decimal places can be entered

input code:

<input id="price" name="price"  class="form-control" type="text" oninput="numFloat(this)">

 

js code:

    //input输入框只能输入数字和 小数点后两位
    function numFloat(obj,val){
        obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
        obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
        obj.value = obj.value.replace(/\.{2,}/g,""); //只保留第一个, 清除多余的
        obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
    }

 

Guess you like

Origin blog.csdn.net/www1056481167/article/details/108262249