• Input钱币规范化


      要求限制input的输入内容,只有输入0-9的数字,backspace,delete,小数点,还有左右移动有效,reFormat函数用于input得到光标时将带逗号的金钱格式转化为没有逗号的字符串,amountInput函数用于控制input能输入的数值大小,其中num指的是小数点后的数字位数,max为小数点之前的数字位数。formatMoney函数用于将字符串转化为对应的金钱格式。

      html

      

    <input type="text" style="200px;ime-mode:disabled;"  onkeydown="return test(event,this,3,3);" onblur="formatMoney(this,3);" onfocus="reFormat(this)" >

     

      js

    <script>     
    function reFormat(obj){
        var CaretPos = 0; 
        var afterFormat;
        if (document.selection) { // IE Support 
                obj.focus(); 
                var Sel = document.selection.createRange(); 
                Sel.moveStart('character', -obj.value.length); 
                CaretPos = Sel.text.length; 
            }else if(obj.selectionStart || obj.selectionStart == '0'){// Firefox support 
                CaretPos = obj.selectionStart; 
        } 
        var strj=obj.value.substring(0,CaretPos);
        var arrj=strj.match(",");
        if(arrj!=null){
            afterFormat=CaretPos-arrj.length;
        }else{
            afterFormat=CaretPos;
        }
        var String=obj.value;
        obj.value=String.replace(/,/g, ""); //删除逗号
        if(obj.setSelectionRange){ //光标定位
            obj.focus();
            obj.setSelectionRange(afterFormat,afterFormat);
        }
        else if (obj.createTextRange) {
            var range = obj.createTextRange();
            range.collapse(true);
            range.moveEnd('character', afterFormat);
            range.moveStart('character', afterFormat);
            range.select();
        }
    }
    function amountInput(event,obj,num,max){
        var n=obj.value;
        var pointPastion=n.lastIndexOf(".");
        var CaretPos = 0; 
        var selectLength;
        var selectValue;
        var numAfterPoint=n.length-pointPastion;
        if (document.selection) { // IE Support 
                obj.focus(); 
                var Sel = document.selection.createRange(); 
                Sel.moveStart('character', -obj.value.length); 
                CaretPos = Sel.text.length; 
                selectLength=document.selection.createRange().text.length;  
                selectValue=document.selection.createRange().text;
            }else if(obj.selectionStart || obj.selectionStart == '0'){// Firefox support 
                CaretPos = obj.selectionStart; 
                if(obj.selectionStart != undefined && obj.selectionEnd != undefined) { 
                    selectLength=obj.value.substring(obj.selectionStart, obj.selectionEnd).length; 
                    selectValue=obj.value.substring(obj.selectionStart, obj.selectionEnd);
                } else { 
                    selectLength=0;
                    selectValue="";
                } 
        } 
        if(selectLength!=0){//覆盖
            if(selectValue.lastIndexOf(".")>=0){//有点
                if((n.length-selectLength)<max){//剩余长度小于MAX
                    if(event.keyCode >= 48 && event.keyCode <= 57){
                        return true;
                    }
                    if (event.keyCode >= 96 && event.keyCode <= 105) {
                        return true;
                    }
                    if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                        return true;
                    }
                }else if((n.length-selectLength)>max){//剩余长度大于MAX
                    if (event.keyCode == 37 || event.keyCode == 39) {
                        return true;
                    }
                }else{//剩余度等于MAX
                    if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                        return true;
                    }
                }
            }else{//无点
                if(event.keyCode >= 48 && event.keyCode <= 57){
                    return true;
                }
                if (event.keyCode >= 96 && event.keyCode <= 105) {
                    return true;
                }
                if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                    return true;
                }
            }
        }else{//插入
            if(pointPastion>=0){//有点
                if(CaretPos>pointPastion){//右边
                    if(numAfterPoint>num){//AfterNum>num
                        if(CaretPos==(pointPastion+1)){//点右边
                            if((n.split(".")[0].length+n.split(".")[1].length)<=max){//B+A<=Max
                                if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                    return true;
                                }
                            }else{//B+A>Max
                                if (event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                    return true;
                                }
                            }
                        }else{//非点右边
                            if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                return true;
                            }
                        }
                    }else{//AfterNum<num
                        if(CaretPos==(pointPastion+1)){//点右边
                            if((n.split(".")[0].length+n.split(".")[1].length)<=max){//B+A<=Max
                                if(event.keyCode >= 48 && event.keyCode <= 57){
                                    return true;
                                }
                                if (event.keyCode >= 96 && event.keyCode <= 105) {
                                    return true;
                                }
                                if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                    return true;
                                }
                            }else{//B+A>Max
                                if(event.keyCode >= 48 && event.keyCode <= 57){
                                    return true;
                                }
                                if (event.keyCode >= 96 && event.keyCode <= 105) {
                                    return true;
                                }
                                if (event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                    return true;
                                }
                            }
                        }else{//非点右边
                            if(event.keyCode >= 48 && event.keyCode <= 57){
                                    return true;
                            }
                            if (event.keyCode >= 96 && event.keyCode <= 105) {
                                return true;
                            }
                            if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                return true;
                            }
                        }
                    }
                }else{//左边
                    if(n.split(".")[0].length>=max){//left>=max
                        if(CaretPos==pointPastion){//点左边
                            if((n.split(".")[0].length+n.split(".")[1].length)<=max){//sum<=max
                                if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                    return true;
                                }
                            }else{//sum>max
                                if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39) {
                                    return true;
                                }
                            }
                        }else{//非点左边
                            if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                return true;
                            }
                        }
                    }else{//left<max
                        if(CaretPos==pointPastion){//点左边
                            if((n.split(".")[0].length+n.split(".")[1].length)<=max){//sum<=max
                                if(event.keyCode >= 48 && event.keyCode <= 57){
                                    return true;
                                }
                                if (event.keyCode >= 96 && event.keyCode <= 105) {
                                    return true;
                                }
                                if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                    return true;
                                }
                            }else{//sum>max
                                if(event.keyCode >= 48 && event.keyCode <= 57){
                                    return true;
                                }
                                if (event.keyCode >= 96 && event.keyCode <= 105) {
                                    return true;
                                }
                                if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39) {
                                    return true;
                                }
                            }
                        }else{//非点左边
                            if(event.keyCode >= 48 && event.keyCode <= 57){
                                return true;
                            }
                            if (event.keyCode >= 96 && event.keyCode <= 105) {
                                return true;
                            }
                            if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                                return true;
                            }
                        }
                    }
                }
            }else{//无点
                var bPos=n.length-CaretPos;
                if(bPos<(num+1)){
                    if (event.keyCode == 110 || event.keyCode == 190){
                        return true;
                    }
                }
                if(n.length>=max){//超长
                    if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                        return true;
                    }
                }else{//未超长
                    if(event.keyCode >= 48 && event.keyCode <= 57){
                        return true;
                    }
                    if (event.keyCode >= 96 && event.keyCode <= 105) {
                        return true;
                    }
                    if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) {
                        return true;
                    }
                }
            }
        }
    return false;
    }
    function formatMoney(obj, n){
            s=obj.value;
            n = n > 0 && n <= 20 ? n : 2;  
            s = parseFloat((s + "").replace(/[^d.-]/g, "")).toFixed(n) + "";
            var l = s.split(".")[0].split("").reverse();
            var r = s.split(".")[1];
            var t = "";
            for(var i = 0; i < l.length; i ++ ) {  
               t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
            }  
            if(obj.value.length==0){
                obj.value="0."
                for(var i=0;i<n;i++){
                    obj.value+="0";
                }
            }else{
                obj.value=t.split("").reverse().join("") + "." + r;  
            }
            
        }
    </script>
  • 相关阅读:
    关于maven的一些记录
    3des和tomcat部署
    java串口通讯
    mina自定义编解码
    Linux 下关闭防火墙设置
    查看本机IP
    linux修改localhost方法
    centos 6.5下使用中文输入法
    linux:can't save files
    ng-model取不到值的问题
  • 原文地址:https://www.cnblogs.com/joesmile/p/5623410.html
Copyright © 2020-2023  润新知