限制input输入符合规则的浮点数

//给指定的dom添加事件
$('#col-sm-2, #min_goods_amount').keydown(function(e){
    return formatInput(e);
});

    
/**
* 限制input输入符合规则的浮点数
* 
* @param {type} e 键盘事件
* @param {type} maxInt 整数位最大位数 默认 6位整数
* @param {type} maxDecimal 小数位最大位数 默认 2位整数
* @returns {Boolean}
*/
function formatInput(e, maxInt, maxDecimal){
    //限制非数字和非点的输入
    if( !$(e.currentTarget).hasClass('limit_angle') ){
        $(e.currentTarget).css('ime-mode','disabled');
        $(e.currentTarget).addClass('limit_angle');
        e.currentTarget.oninput = function(){
            if(/[^\.|\d]/g.test(this.value)){
                this.value = this.value.replace(/[^\.|\d]/g, '');
            }
        }
    }
    
    //限制浮点数规则
    maxInt = maxInt || 6;
    maxDecimal = maxDecimal || 2;

    e.keyCode = e.keyCode || e.charCode;
   
    //当前的值
    var val = $(e.currentTarget).val();
    //输入的值
    var input = String.fromCharCode(e.keyCode);
    //小数点的位置
    var pointPosition = val.indexOf('.');
    //光标的位置
    var seletorPosition = getCursorPosition( e.currentTarget );
    //判断小数点的变量
    var n = val.split('.');

    //上下左右键
    if(e.keyCode >=37 && e.keyCode <=40){
        return true;
    }
    
    //输入的是退格键
    if(e.keyCode == 8){
        //有小数点,并且整数位就一位,这时删除整数位,要那0补上
        if(pointPosition == 1 && seletorPosition == 1){
            $(e.currentTarget).val('0' + val.substr(pointPosition));
            setSeletorPosition(e.currentTarget, 1);
            return false;
        }

        //如果有小数点,如果整数位加上小数位的位数大于最大整数位,这时不让删除小数点,以免出现规则外的整数位
        if(pointPosition !=-1 && n[0].length + n[1].length > maxInt && (pointPosition + 1) == seletorPosition){
            return false;
        }

        return true;
    }
    
    //delete 键
    if( e.keyCode == 46 ){
        //有小数点,并且整数位就一位,这时删除整数位,要那0补上
        if(pointPosition == 1 && seletorPosition == 0){
            $(e.currentTarget).val('0' + val.substr(pointPosition));
            setSeletorPosition(e.currentTarget, 0);
            return false;
        }
        
        //如果有小数点,如果整数位加上小数位的位数大于最大整数位,这时不让删除小数点,以免出现规则外的整数位
        if(pointPosition !=-1 && n[0].length + n[1].length > maxInt && (pointPosition) == seletorPosition){
            return false;
        }
        
        return true;
    }

    
    //input中已经有值,则不允许在第一个位置输入0
    if(e.keyCode == 48 && seletorPosition==0 && val != ''){
        return false;
   }

    //输入的是小数点
    if(e.keyCode == 190 || e.keyCode == 46){
        //小数点不能再首位
        if(seletorPosition == 0) return false;

        //已经输入过小数点
        if(pointPosition != -1) return false;

        //光标所在不能是遗留大于maxInt位的整数
        if(val.substr(0, seletorPosition).length > maxInt) return false;
        //光标所在不能是遗留大于maxDecimal位的小数
        if(val.substr(seletorPosition).length > maxDecimal) return false;

        return true;
    }

    //输入的不是数字
    if( e.keyCode < 48 || e.keyCode > 57 ){
        return false;
    }

    //光标在第一位,整数位是0 ,这是输入数值是直接替换0
    if(seletorPosition == 1 && parseInt(n[0]) == 0){
        if(pointPosition != -1){
            $(e.currentTarget).val(input + val.substr(pointPosition));
        }else{
            $(e.currentTarget).val(input);
        }
        
        setSeletorPosition(e.currentTarget, 1);
        return false;
    }

    //如果有小数点
    if(pointPosition > 0){
        //光标在小数点前面,判断小数位的大小
        if(pointPosition < seletorPosition){
            if(n[1] == "undefined") return true;
            //小数位的位数
            return n[1].length < maxDecimal;
        }
    }

    //整数位的位数
    return n[0].length < maxInt;
}

//获得光标所在input的位置
function getCursorPosition(dom){
   var cursurPosition=-1;
   if(typeof dom.selectionStart != "undefined"){//非IE浏览器
       cursurPosition= dom.selectionStart;
   }else{//IE
       var range = document.selection.createRange();
       range.moveStart("character",-dom.value.length);
       cursurPosition=range.text.length;
   }
   return cursurPosition;
}

//设置光标所在input的位置
function setSeletorPosition(dom, index){
   if (dom.setSelectionRange) { // 标准浏览器
       dom.setSelectionRange(index, index)    
   } else { // IE9-
       var len = dom.value.length;
       var range = dom.createTextRange()
       range.moveStart("character", -len)
       range.moveEnd("character", -len)
       range.moveStart("character", index)
       range.moveEnd("character", 0)
       range.select()
   }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值