function f_check_integer(obj)   
{          
    if (/^(\+|-)?\d+$/.test( obj ))    
    {   
       return true;   
    }    
    else    
    {   
        return false;   
    }   
}
if(f_check_integer(space) ){
         mesg = mesg + "只能输入整数!\n";
    }

解决方案 »

  1.   

    用正则表达式可以并且比较通用,如果楼主使用了一些js框架的话可能会有一些validation机制,可以直接调用。
      

  2.   

    <script language=jscript>
    function onKeyDown() {
        var char = String.fromCharCode(event.keyCode);
        var re = /[0-9]/g;
        event.returnValue = char.match(re) != null ? true : false
    }function onPaste() {
        var text = window.clipboardData.getData("text");
        var re = /^\d+$/;
        event.returnValue = text .match(re) != null ? true : false}
    </script>
      

  3.   

    晕!整反了function f_check_integer(obj)  
    {          
        if (/^(\+|-)?\d+$/.test( obj ))    
        {  
          return true;  
        }    
        else    
        {  
            return false;  
        }  

    if(!f_check_integer(space) ){ 
            mesg = mesg + "只能输入整数!\n"; 
        } 
      

  4.   

    我是菜菜我不会jscript请在我的代码上面改下谢谢了
      

  5.   

    if (! /^(\+|-)?\d+$/.test( space )){ 
    mesg = mesg + "磁盘空间只能输入整数!\n"; 

    虽然不能输入字符了 
    可是还能输入小数 
    是为什么?
      

  6.   


    <input type="text" onkeypress="return chk()" />
    <script>
    function chk(e){
    e?intKey=e.which:intKey=event.keyCode;
    return !/\D/.test(String.fromCharCode(intKey));
    }
    </script>
      

  7.   


    <input type=text id="txtUnit"     onkeypress="return &nbsp; event.keyCode>=48&amp;&amp;event.keyCode<=57" onpaste="return &nbsp; !clipboardData.getData('text').match(/\D/)" ondragenter="return &nbsp; false" onkeyup="value=value.replace(/^0*/, &nbsp; ');if(value>100)value=100;" value=''   style="IME-MODE: disabled;">
    呵呵 强力建议用用看 保准你满意. 如果还有其他需要搂住自己可以改一下
      

  8.   

    刚刚写的有点小问题,这样
    <input type="text" onkeypress="return chk(event)" />
    <script>
    function chk(e){
    e.which?intKey=e.which:intKey=event.keyCode;
    return intKey>=48&&intKey<=57;
    }
    </script>
      

  9.   

    刚刚写的有点小问题,这样
    <input type="text" onkeypress="return chk(event)" />
    <script>
    function chk(e){
    e.which?intKey=e.which:intKey=event.keyCode;
    return intKey>=48&&intKey<=57;
    }
    </script>
      

  10.   

    FF下退格键也有值,这样<input type="text" onkeypress="return chk(event)" />
    <script>
    function chk(e){
    e.which?intKey=e.which:intKey=event.keyCode;
    return intKey>=48&&intKey<=57||intKey==8;
    }
    </script>
      

  11.   

    能不能在输错的时候还有个提示呀?
    if(! /^(\w)*$/.test( space ) ) {
    mesg = mesg +"只允许为英文和数字的混合"
        }
    这个是只能是英文和数字!怎么才能让英文也不能输入呀?
      

  12.   


    <script language="javascript"> 
    function checklogin() 

    var space = parseInt(document.login.pw_space.value, 10); if (!/^(\+|-)?\d+$/.test( space )){ 
    mesg = mesg + "磁盘空间只能输入整数!\n"; 

    if (mesg != ""){ 
    mesg = mesg ;;//请填写好以下各项 
    alert(mesg); 
    return false; 

    login.submit(); 

    </script> 
    <FORM name="login" method="post" action="NewUserok.php" onsubmit="return checklogin();"> 
    <tr> 
    <td align="right" bgcolor="#EEF5FD">磁盘空间:</td> 
    <td height="30" align="left" bgcolor="#EEF5FD" style="padding-left:5px;"><input name="pw_space" type="text" id="pw_space" value="<?php echo $buffer;?>" size="6" /> 
    <span class="STYLE1">G</span></td> 
    </tr> 
    <tr> 
    <td height="81" align="center" valign="bottom"><input name="pw_space1"  type="submit" value="确 定" border="0" class="btn"></td>
        </tr>
    </table>
    高手看下哪里有错误呀!
    依然可以输入小数呀