限制input 只能输入正数和零

$('#当前input').on('input propertychange', function () {
    var thisValue = $(this).val();
    //输入值了
    if(thisValue){
        //如果输入的不是.或者不是数字,替换 g:全局替换
        thisValue = thisValue.replace(/[^(\d|.)]/g,"");
        //不能有两个点
        if(thisValue.indexOf(".")>-1 && (thisValue.substring(thisValue.indexOf(".")+1,thisValue.length)).indexOf(".")>-1){
            //保留第一个点和之前的数据
            thisValue = thisValue.substring(0,thisValue.indexOf(".",thisValue.indexOf(".")+1));
        }
        //点出现在第一位就变成零点
        if(thisValue.indexOf(".") == "0"){
            thisValue = thisValue.replace(".","0.")
        }
        //00,01 等变成 0 ,1 ...
        if(thisValue=="00" || thisValue=="01" || thisValue=="02" || thisValue=="03" || thisValue=="04" || thisValue=="05" || thisValue=="06" || thisValue=="07" || thisValue=="08" || thisValue=="09"){
            thisValue = thisValue.replace("0","");
        }
        $(this).val(thisValue);
    }
})

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值