我在text里要输入金额,格式是xx.xx,小数点后必须有两位,如果用户输入的不是数字,便不能显示,如果用户输入的是xx,则自动变成xx.00,如果用户输入的是xx.x则自动变马xx.x0 
请指点下,谢谢了

解决方案 »

  1.   


    function commafy(oInput){
        var re=/(-?\d+)(\d{3})/
        var num = oInput.value.replace(/,/g,"");
        while(re.test(num)){
                num=num.replace(re,"$1,$2")
            }
            oInput.value=num
    }
     //这段代码没有转成XX.XX,只是做到了千分位分割,我需要每次转换后光标在.前面,只有输入.号时,才会出现小数点后面的值
      

  2.   


    JS能做到这个吗?我用ASP.NET的文本框控件能够做到这个.
    JS好像做不到吧?
      

  3.   

    ext中的NumberField可以实现你说的这种功能,你可以参考一下这里面的代码!
      

  4.   


    /**
     * @param o 文本框对象
     * @param n 保留几位小数
     */
    function fixNumber(o,n){
        if(o.value==""||isNaN(o.value)||o.value==Infinity){
            o.value=parseFloat("0").toFixed(n);
        }else{
            o.value=parseFloat(o.value).toFixed(n);
        }
    };
    html中
    <input type="text" onkeyup="fixNumber(this,2)"/>
      

  5.   

    用onkeydown 和onblur 两个函数
      

  6.   

    如果用户输入的不是数字,便不能显示这个JS可以做到。
    onkeydown里面判断。如果不是数字,就return false;
    忘记是不是这样了。
      

  7.   

    <html>
    <head>
    <script language="javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js" ></script>
    <script language="javascript">
    $(document).ready(function(){
     $("#textId").keypress(function(event){
       if(event.keyCode&&(event.keyCode<48||event.keyCode>57)){
          if(event.keyCode == 46&&!/\./.test($(this).val())){
           var currentVal = $(this).val();
           if(!isNaN(parseInt(currentVal))){
            $(this).val(currentVal+".");  
           }
           else{
            $(this).val("0.");
           }
          }
        event.preventDefault();
       }
     }).blur(function(){
        var the_Val = parseFloat($(this).val());
        if(!isNaN(the_Val)){
        if(!/\./.test($(this).val())){
          $(this).val(the_Val+".00");
        }
        else{
          $(this).val(the_Val.toFixed(2));
        }
       }
     });
    });
    </script>
    </head>
    <body>
    <input type="text" id="textId" ></input>
    </body>
    </html>功能:
    只能输入数字和小数点;
    若一开始就输入小数点,则默认个位上的数为0,即 0. ;
    小数点仅能输入一次,不输入则默认为xx.00
      

  8.   

    <html>
    <head>
    <script language="javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js" ></script>
    <script language="javascript">
    $(document).ready(function(){
    //千分位处理函数 
     var departNum = function(textVal,the_other){
       var the_array = [];
       var i = 0;
       the_array.push(textVal.slice(textVal.length-2,textVal.length));
       for(i=textVal.length-5;i>=0;i-=3){
         the_array.push(textVal.slice(i,i+3));
       }
       if(0-i<3){the_array.push(textVal.slice(0,3+i));}
       for(var k=the_array.length-1;k>=0;k--){
        the_other.push(the_array[k]);
       }
     }
    //输入限制
     $("#textId").keypress(function(event){
       var current = $(this).val();
       if(event.keyCode&&(event.keyCode<45||(event.keyCode>45&&event.keyCode<48)||event.keyCode>57)){
          if(event.keyCode == 46&&!/\./.test(current)){
           if(!isNaN(parseInt($(this).val().replace(/,/,"")))){
            $(this).val(current+".");  
           }
           else{
            $(this).val("0.");
           }
          }
        event.preventDefault();
       }
       else{
         if(event.keyCode == 45&&(/-/.test(current)||/\d/.test(current))){event.preventDefault();}
         else{
           if(!/\./.test(current)){
            var the_new = $(this).val().replace(/,/g,"");
            var theArray = [];
            var theFlag = "";
            if(/-/.test(current)){theFlag = the_new.slice(0,1);the_new = the_new.slice(1);}
            if(parseInt(the_new) >= 100){
              departNum(the_new,theArray);
              $(this).val(theFlag+theArray.join(","));
            }
           }
         }
       }
     }).blur(function(){
        var the_Val = $(this).val().replace(/,/g,"");
        if(!isNaN(parseFloat(the_Val))){
        if(!/\./.test(the_Val)){
          var theArray = [];
          var theFlag = "";
          var the_one = the_Val.slice(-1);
          var the_new = the_Val.replace(/\d$/,"");
          if(/-/.test(the_Val)){theFlag = the_new.slice(0,1);the_new = the_new.slice(1);}
          if(parseInt(the_new) >= 100){
            departNum(the_new,theArray);
            $(this).val(theFlag+theArray.join(",")+the_one+".00");
          }
          else{
            $(this).val(the_Val+".00");
          }
        }
        else{
          var theArray = [];
          var theFlag = "";
          var the_now = parseFloat(the_Val).toFixed(2);
          var the_nowStr = String(the_now).slice(-4);
          var the_new = String(the_now).replace(/\d\.\d\d/,"");
          if(/-/.test(the_Val)){theFlag = the_new.slice(0,1);the_new = the_new.slice(1);}
          if(parseInt(the_new) >= 100){
            departNum(the_new,theArray);
            $(this).val(theFlag+theArray.join(",")+the_nowStr);
          }
          else{
            $(this).val(String(the_now));
          }
        }
       }
     });
    });
    </script>
    </head>
    <body>
    <input type="text" id="textId" ></input>
    </body>
    </html>
    作了些修改,可以实现千分位逗号区分和输入负数
      

  9.   


    非常感谢CLinHF,经过测试,有2点小小的瑕疵外,基本符合要求
    1.负号只能第一个字母输入,用户如果输入到一半才发现应该是负数,则需要全部删除重新输入,对用户来说肯定会有点不满意的。
    2.只有失去焦点的时候才进行小数的判断和补充,是否可以做到和千分位一样每一次按键都进行格式化并返回,光标默认在小数点前面,当按下小数点符号后输入的数字自动替换小数点后的数字。===========================
    (第二个只是我了解的最理想状态,很可能是因为控件的原因。第一个还是比较重要的,希望可以继续完善下)
      

  10.   

    针对第一个Bug进行了修改,负号问题已解决,新代码如下:<html>
    <head>
    <script language="javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js" ></script>
    <script language="javascript">
    $(document).ready(function(){
    //千分位处理函数 
     var departNum = function(textVal,the_other){
       var the_array = [];
       var i = 0;
       the_array.push(textVal.slice(textVal.length-2,textVal.length));
       for(i=textVal.length-5;i>=0;i-=3){
         the_array.push(textVal.slice(i,i+3));
       }
       if(0-i<3){the_array.push(textVal.slice(0,3+i));}
       for(var k=the_array.length-1;k>=0;k--){
        the_other.push(the_array[k]);
       }
     }
    //输入限制
     $("#textId").keypress(function(event){
       var current = $(this).val();
       if(event.keyCode&&(event.keyCode<45||(event.keyCode>45&&event.keyCode<48)||event.keyCode>57)){
          if(event.keyCode == 46&&!/\./.test(current)){
           if(!isNaN(parseInt($(this).val().replace(/,/,"")))){
            $(this).val(current+".");  
           }
           else{
            $(this).val($(this).val()+"0.");
           }
          }
        event.preventDefault();
       }
       else{
         if(event.keyCode == 45&&/-/.test(current)){event.preventDefault();}
         else if(event.keyCode != 45){
           if(!/\./.test(current)){
            var the_new = $(this).val().replace(/,/g,"");
            var theArray = [];
            var theFlag = "";
            if(/-/.test(current)){theFlag = the_new.slice(0,1);the_new = the_new.slice(1);}
            if(parseInt(the_new) >= 100){
              departNum(the_new,theArray);
              $(this).val(theFlag+theArray.join(","));
            }
           }
         }
       }
     })
    .keyup(function(event){
       if(event.keyCode == 109&&$(this).val().slice(0,1) != "-"){
          var the_Real = $(this).val();
          $(this).val(the_Real.replace(/-/,""));
       }
     })
    .blur(function(){
        var the_Val = $(this).val().replace(/,/g,"");
        if(!isNaN(parseFloat(the_Val))){
        if(!/\./.test(the_Val)){
          var theArray = [];
          var theFlag = "";
          var the_one = the_Val.slice(-1);
          var the_new = the_Val.replace(/\d$/,"");
          if(/-/.test(the_Val)){theFlag = the_new.slice(0,1);the_new = the_new.slice(1);}
          if(parseInt(the_new) >= 100){
            departNum(the_new,theArray);
            $(this).val(theFlag+theArray.join(",")+the_one+".00");
          }
          else{
            $(this).val(the_Val+".00");
          }
        }
        else{
          var theArray = [];
          var theFlag = "";
          var the_now = parseFloat(the_Val).toFixed(2);
          var the_nowStr = String(the_now).slice(-4);
          var the_new = String(the_now).replace(/\d\.\d\d/,"");
          if(/-/.test(the_Val)){theFlag = the_new.slice(0,1);the_new = the_new.slice(1);}
          if(parseInt(the_new) >= 100){
            departNum(the_new,theArray);
            $(this).val(theFlag+theArray.join(",")+the_nowStr);
          }
          else{
            $(this).val(String(the_now));
          }
        }
       }
     });
    });
    </script>
    </head>
    <body>
    <input type="text" id="textId" ></input>
    </body>
    </html>因为现在比较忙,所以对于你的第二个要求:
    等有时间,再进行完善!
      

  11.   

    主要的修改是:添加了keyup函数,对输入后,负号的位置进行判断,并做出处理。
      

  12.   

    我也试试 <INPUT TYPE="text" NAME="" onkeyup="checkinp(this)">
      <SCRIPT LANGUAGE="JavaScript">
      <!--
    function checkinp(oInput){
    var reg = /^\-?([\d\,]+\.?\d{0,2}?)?$/;
    var num = oInput.value;
    /* 取光标位置 */
    var s = document.selection.createRange(); 
    var r = oInput.createTextRange();
    s.setEndPoint( "StartToStart",r);
    var curP = s.text.length;
    if (!reg.test(num)) {
    if (curP <= num.indexOf(".")+1 || /\.(\D|\d\D)/.test(num)) {
    oInput.value = num.substring(0,curP-1) + num.substring(curP,num.length);
    if (curP != num.indexOf(".")+1) curP -= 1;
    }
    else oInput.value = num.substring(0,num.length-1);
    }
    else {
    if (oInput.value=="-") return;
    num = num.replace(/,/g,"");
    num = parseFloat(num).toFixed(2);
    var re = /(-?\d+)(\d{3})/;
    while(re.test(num)){
       num = num.replace(re,"$1,$2")
    }
    oInput.value=num;
    if(curP <= num.indexOf(".") ) curP = num.indexOf(".");
    }
    //恢复光标位置
    r.moveStart( 'character',curP); 
    r.collapse(true); 
    r.select(); 
    }
      //-->
      </SCRIPT>
      

  13.   

    嗯,onkeyup会有闪烁
    不想闪烁的话就得像12楼那样onkeypress,然后判断keycode……
      

  14.   


    一直按住,就没有onkeyup,就不执行函数了……
      

  15.   

    改了改,好像解决了27楼的问题
    还是得用onkeypress啊…… <INPUT TYPE="text" style="ime-mode:disabled" onkeydown="if(event.repeat)event.returnValue=false;//屏蔽连按" onkeypress="initPrice(this)" onblur="checkinp(this)">
      <SCRIPT LANGUAGE="JavaScript">
      <!--
    function initPrice(oInput){
    var num = oInput.value;
    /* 取光标位置 */
    var s = document.selection.createRange(); 
    var r = oInput.createTextRange();
    s.setEndPoint( "StartToStart",r);
    var curP = s.text.length; num = num.substring(0,curP) + String.fromCharCode(event.keyCode) + num.substring(curP);
    var reg = /^\-?([\d\,]*\.?\d{0,3}?)?$/;
    // 0~9:48~57; \,:44; \-:45; \.:46; 
    if ( event.keyCode<44 || event.keyCode>57 || event.keyCode==47 || !reg.test(num) ) {
    if (event.keyCode==46) curP = num.indexOf(".")+1;
    event.returnValue = false;
    }
    else {
    curP += 1;
    if (num=="-") return;
    if (/\.\d{3}$/.test(num)) num = num.substring(0,num.length-1);
    event.returnValue = false;
    num = num.replace(/,/g,"");
    num = parseFloat(num).toFixed(2);
    var re = /(-?\d+)(\d{3})/;
    while(re.test(num)){
       num = num.replace(re,"$1,$2")
    }
    oInput.value=num;
    if(num.indexOf(".")>0 && curP <= num.indexOf(".") ) curP = num.indexOf(".");
    }
    //恢复光标位置
    r.moveStart( 'character',curP); 
    r.collapse(true); 
    r.select(); 
    }
    function checkinp(obj){
    var num = obj.value.replace(/,/g,"");
    num = parseFloat(num).toFixed(2);
    var re = /(-?\d+)(\d{3})/;
    while(re.test(num)){
       num = num.replace(re,"$1,$2")
    }
    obj.value=num;
    }
      //-->
      </SCRIPT>
      

  16.   


    还有不支持火狐浏览器。
    还有“CLinHF”的方法也不支持火狐浏览器。