<script language="javascript"> 
function havenonumber(theelement) {
text="1234567890";
for(i=0;i<=theelement.length-1;i++) {
char1=theelement.charAt(i);
index=text.indexOf(char1);
if(index==-1) {
return true; 
}
}
return false;
}
         function  checkform() {
            if ((havenonumber(document.form1.number.value))){
                 return false;
            }
         return ture;
         }
</script>
<form method="POST" name="form1" action="1.jsp" onSubmit="return checkform()">
<input type="text" name="number" size=20>
</form>

解决方案 »

  1.   

    不好意思,刚才写好没测试,写错了一个true,再贴一遍,这次测试了的
    <script language="javascript"> 
    function havenonumber(theelement) {
    text="1234567890";
    for(i=0;i<=theelement.length-1;i++) {
    char1=theelement.charAt(i);
    index=text.indexOf(char1);
    if(index==-1) {
    return true; 
    }
    }
    return false;
    }
             function  checkform() {
                if ((havenonumber(document.form1.number.value))){
         alert("输入不对");
                     return false;
                }
             return true;
             }
    </script>
    <form method="POST" name="form1" action="1.jsp" onSubmit="return checkform()">
    <input type="text" name="number" size=20>
    </form>
      

  2.   

    <INPUT TYPE="text" onchange="checkit(this)"><SCRIPT LANGUAGE="JavaScript">
    <!--
    function checkit(e){
    for(i=0;i<e.value.length;i++){
    if(e.value.charCodeAt(i)<48 || e.value.charCodeAt(i)>57){
    alert("请输入半角数字");
    return;
    }
    }
    }
    //-->
    </SCRIPT>
      

  3.   

    IE 5 以上限制半角很简单只要这样:<INPUT TYPE = text STYLE = "ime-mode:disabled" >不过要限定是数字就必须用脚本。ime-mode Attribute | imeMode Property
    --------------------------------------------------------------------
    Sets or retrieves the state of an Input Method Editor (IME).SyntaxHTML { ime-mode : sMode }  
    Scripting object.style.imeMode [ = sMode ] Possible ValuessMode    String that specifies or receives one of the following values.auto      Default. IME is not affected. This is the same as not specifying the ime-mode attribute. 
    active    All characters are entered through the IME. Users can still deactivate the IME. 
    inactive  All characters are entered without IME. Users can still activate the IME. 
    disabled  IME is completely disabled. Users cannot activate the IME if the control has focus.