欢迎加入站点基地设计开发交流群:30720409
详细内容
判断文本框必须填写整数,或者必须填写数字,可有小数的方法
发布日期:2011-11-29     点击:3457     字体:[ ]

文本框:

 <input type="text" name="textfield"  onchange="if(this.value.match(/^0+$/g)) this.value=0; else this.value = this.value.replace(/^0+/g,'');"
                                    onblur="if (this.value.indexOf('.') == 0) {this.value = '0' + this.value;} this.value = FormatNumber(this.value,2);"
                                  onkeypress="return checkData(event,this);"   onpaste="return clipboardData.getData('text').match(/^\d+\.{0,1}\d{0,precision}$/gi) != null"
                                    ondragenter="return false;" />

这里调用了FormatNumber函数,功能是将数字转化为千分位,并格式化为有两位小数格式,具体可自己指定,也可不用该功能

checkData函数判断数字,拒绝非数字输入,但可输入小数点。

以下为具体用到的JS函数,可定义在一个JS文件里,方便调用:

<script language="javascript">
function FormatNumber(strInput,precision) {
            //将输入参数转换为字符串形式
            //var strInput = Math.abs(intInput).toString();
            //如果有小数,把小数部分提取出来
            if(strInput.length<=0)
            {
                return "";
            }
            var strXS = "";
            var strZS ="";
            strInput = strInput.replace(',','');
            if (strInput.indexOf(".", 0) != -1) {
                strXS = strInput.substring(strInput.split(".")[0].length, strInput.length);
                strZS = strInput.split(".")[0];
            }
            else
            {
                strZS = strInput;
                strXS = ".0";
            }
            //获取输入参数的长度
            var iLen = strZS.length;
            //如果输入参数的长度小于等于3,则直接返回
            //否则,再进行处理
            if (iLen > 3) {
                    //首先取,以作为起始点,每3位截取一次存入数组,最后再进行拼接返回
                    var iMod = iLen % 3;
                    //每3位截取的起始点 
                    var iStart = iMod;
                    //每3位截取的存储数组
                    var aryReturn = [];

                    //循环处理:每3位截取一次 存储到数组
                    while (iStart + 3 <= iLen) {
                        aryReturn[aryReturn.length] = strZS.substring(iStart, iStart + 3);
                        iStart = iStart + 3;
                    }
                    //将数组中的数据连接起来
                    aryReturn = aryReturn.join(",");

                    //处理输入参数长度不是3的倍数的情况
                    if (iMod != 0) {
                        aryReturn = strZS.substring(0, iMod) + "," + aryReturn;
                    }
                    strZS = aryReturn;
                }
                while(strXS.length<precision+1)
                {
                    strXS=strXS+"0";
                }
               
                if(precision<=0)
                {
                    return strZS;
                }
                return strZS + strXS;
        }
  
   function checkData(e,obj)
        {
            var precision =2;
            var pre = Number(precision);
            var key = window.event ? e.keyCode : e.which;
            //选中内容处理
            var len = GetChooseTextLength(obj);
            if(key <= 57 && key >= 48 && len>0 )
            {
                return true;
            }
            if(len>0 && IsChooseTextHavePoint(obj) && key == 46)
            {
                return true;
            }
            //非IE
            if(obj.selectionStart)
            {
                if(key <= 57 && key >= 48 && obj.value.indexOf('.')>=0 && obj.selectionStart <=obj.value.indexOf('.'))
                {
                    return true;
                }
                else if(obj.value.indexOf('.')<0 && ( obj.value.length - obj.selectionStart >2) && key ==46)
                {
                    return false ;
                }
            }
            //IE
            else
            {
                var index = GetCursorPsn(obj);
                if(key <= 57 && key >= 48 && obj.value.indexOf('.')>=0 && index <=obj.value.indexOf('.'))
                {
                    return true;
                }
                else if(obj.value.indexOf('.')<0 && (obj.value.length - index >2) && key ==46)
                {
                    return false;
                }
            }
            return ((key == 8) || (key == 9)  || (key == 0) || (key == 13)
                || ((key <= 57 && key >= 48) && (pre <= 0 || (pre > 0 && (obj.value.indexOf('.') < 0
                || (obj.value.indexOf('.') >= 0 && (obj.value.length - obj.value.indexOf('.')) <= pre)))))
                || (pre > 0 && key == 46 && obj.value.indexOf('.') < 0));
        }

function GetChooseTextLength(obj)
        {
            if(document.selection)
            {
                var slct = document.selection;
                var rng = slct.createRange();
                return rng.text.length;
            }
            else
            {
                if(obj.selectionEnd - obj.selectionStart>0)
                    return obj.selectionEnd - obj.selectionStart ;
                else
                    return obj.selectionStart - obj.selectionEnd ;
            }
        }
       
        function GetCursorPsn(obj)
        {
            var slct = document.selection;
            var rng = slct.createRange();
            obj.select();
            rng.setEndPoint("StartToStart",slct.createRange());
            var psn = rng.text.length;
            rng.collapse(false);
            rng.select();
            return psn;
       
        }

 function IsChooseTextHavePoint(obj)
        {
            if(document.selection)
            {
                var slct = document.selection;
                var rng = slct.createRange();
                return rng.text.indexOf('.')>=0;
            }
            else
            {
                var selstart = obj.selectionStart; //文字浮标选择的开始位置
                var selend = obj.selectionEnd;
                return obj.value.substring(selend,selstart).indexOf('.')>=0;
            }
        }

 


用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号-1 可人软件设计