试试
<input type="text" name="o_phone" size="12" onchange="if(/\D/.test(o_phone.value)){alert('只能输入数字');this.value='';this.focus();}">

解决方案 »

  1.   

    <script>
    function check(){
      var price = form1.unit_price.value;
      if(!(price.match(/\D/)==null))
      {
      alert("不全是数字!");
      document.form1.unit_price.focus();
      return false;
      }
    }
    </script>
    <form name=form1>
    <input type="text" name="unit_price" size="12" onblur=check()>
    <input type="text" name="o_phone" size="12">
    </form>
      

  2.   

    <html>
    <head>
    <script language="javascript">
    function check_num()
    {
      value1=document.form1.text1.value;
      if(isNaN(value1)){alert('输入字符非法');}
    }
    </script>
    </head>
    <body>
    <form name=form1 method='post'onsubmit="check_num()" >
    <input type=text name=text1>
    </form>
    </body>
    </html>