<input type="text" name="text"  value="0" onkeyup="value=value.replace(/[^\d]/g,'')"/>
怎样让他只能输入整数?例如:禁止输入0123456。

解决方案 »

  1.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">
    /**
                 * author: develop_design_level
                 * date: 2009-10-30
                 * @param {Object} id
                 */
                function $(id){
                    if (document.getElementById) {
                        return document.getElementById(id);
                    }
                    else {
                        return document.all.id;
                    }
                }
    function checkInputText(){
    var regExp = /^\d+$/g;
    var text = this.value;
    if(!regExp.test(text)){
    this.value = '0';
    }
    }
    window.onload = function(){
    $('testId').onkeyup = checkInputText;
    };
    </script>
    </head>
    <body>
    <form>
    <input type="text" id="testId" value="0">
    </form>
    </body>
    </html>
      

  2.   

    这样效果好<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
    </head>
    <body>
    <script>
        function regInput(obj, reg, inputStr) {
            var docSel = document.selection.createRange()
            if (docSel.parentElement().tagName != "INPUT") return false
            oSel = docSel.duplicate()
            oSel.text = ""
            var srcRange = obj.createTextRange()
            oSel.setEndPoint("StartToStart", srcRange)
            var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length);
            return reg.test(str)
        }
    </script>
    <input type="text" id="txt" onkeypress    = "return regInput(this,/^\d*$/,String.fromCharCode(event.keyCode))"></body>
    </html>
      

  3.   

    按照楼主的原意
    <input type="text" name="text"  value="0" onkeyup="value=value.replace(/[^\d]/g,'').replace(/^0(\d+)$/,'$1')"/>