3

下面我使用了我的代码,我想只允许一个数字或两个小数点,如下所示。

有效号码。

1
2.34
9.78

无效号码。

10
1.234
99.1234

用户不能插入无效号码。

我使用了下面的代码。

<script src="mos-css/jquery-latest.js"></script>
<script>
document.write('<input id="inputField" onkeyup="run(this)"  />');
function run(field) {
    setTimeout(function() {
       // var regex =  /^\d{1}\.\d{2}$/;
      // var regex = /^[0-9]+(\.[0-9]{5})?$/;
    var regex =/^[0-9](\.\d{2})?$/;
    ///^\d+(\.\d{5})?$/;
        field.value = regex.exec(field.value);
      // field.value = regex.exec(field.value);
    }, 5);
}
</script>

谢谢大家。

4

4 回答 4

2
<html>  
<head>  
<html>   
  <head>     
    <script>  
      // Retrieve last key pressed.  Works in IE and Netscape.  
      // Returns the numeric key code for the key pressed.  
      function getKey(e)  
      {  
        if (window.event)  
           return window.event.keyCode;  
        else if (e)  
           return e.which;  
        else  
           return null;  
      }  
      function restrictChars(e, obj)  
      {  
        var CHAR_AFTER_DP = 2;  // number of decimal places  
        var validList = "0123456789.";  // allowed characters in field  
        var key, keyChar;  
        var char=0;
        key = getKey(e);  
        if (key == null) return true;  
        // control keys  
        // null, backspace, tab, carriage return, escape  
        if ( key==0 || key==8 || key==9 || key==13 || key==27 )  
           return true;  
        // get character  
        keyChar = String.fromCharCode(key);  
        // check valid characters   
        if (validList.indexOf(keyChar) != -1)  
        {  
          // check for existing decimal point  
          var dp = 0;
          if(obj.value.indexOf( ".")==-1){

              // room for more after decimal point?  
              if( obj.value.length - dp <= char)  {
                return true;  }
                else{
                if(keyChar == ".")  
                    return true;  
                else
                    return false;
                }
          }
          if((dp = obj.value.indexOf( ".")) > -1)  
          { 
            if(keyChar == ".")  
              return false;  // only one allowed  
            else  
            {  
              // room for more after decimal point?  
              if( obj.value.length - dp <= CHAR_AFTER_DP)  
                return true;  
            }  
          }  
          else {return true};  
        }  
        // not a valid character  
        return false;  
      }  
    </script>  
  </head>  
  <body>  
    <form name="test">  
      Input Money Amount $<input type="text" name="money" onKeyPress="return restrictChars(event, this)">  
    </form>  
  </body>  
</html>

试试看代码

于 2013-05-04T07:07:49.813 回答
1

为什么不接受所有输入,然后执行:

field.value.toFixed(2);
于 2013-05-04T06:03:53.587 回答
1

你可以使用这个:

/^\d(?:\.\d{2})?$/

例子:

if (/^\d(?:\.\d{2})?$/.test(myString)) alert('true');
else alert('false');
于 2013-05-04T06:07:23.920 回答
1

我尝试了ankit的代码,发现它不能正常工作,所以在下面包含了一个固定版本:

  function getKey(e) 
  { 
    if (window.event) 
       return window.event.keyCode; 
    else if (e) 
       return e.which; 
    else 
       return null; 
  } 
  function restrictChars(e, obj) 
  { 
    var CHAR_AFTER_DP = 2;  // number of decimal places

    var validList = "0123456789.";  // allowed characters in field 
    var key, keyChar; 
    key = getKey(e); 
    if (key == null) return true; 
    // control keys 
    // null, backspace, tab, carriage return, escape 
    if ( key==0 || key==8 || key==9 || key==13 || key==27 ) 
       return true; 
    // get character 
    keyChar = String.fromCharCode(key); 
    // check valid characters  
    if (validList.indexOf(keyChar) != -1) 
    { 
      // check for existing decimal point 
      var dp = 0;
      if(obj.value.indexOf( ".")==-1){
          return true; 
      }
      if((dp = obj.value.indexOf( ".")) > -1) 
      {
        if(keyChar == ".") 
          return false;  // only one allowed 
        else 
        { 
          // room for more after decimal point? 
          if( obj.value.length - dp <= CHAR_AFTER_DP) 
            return true; 
        } 
      } 
      else {return true}; 
    } 
    // not a valid character 
    return false; 
  }
于 2013-08-02T14:26:08.113 回答