<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function checknum(){
var dig,count=0;
var num=document.form1.text3.value;
if(num==""){
alert('请输入数字')
}
else{
for(i=0;i<num.length;i++){
dig=num.charAt(i);
if(dig<'0'||dig>'9'){
alert('输入的必须为数字');
return false;
}
}
}
}function checktext3(){
document.form1.text3.value="";
}
</script><style type="text/css"></style>
</head>
 
<body>
<form name="form1">
    <p>检查是否有数字:
    <input type="text" name="text3" value="请输入数字" onclick="checktext3()" />
    <input type="button" name="button3" value="检查是否有数字" onclick="checknum()"/>
    </p>
 </form>
</body>
</html>
  

解决方案 »

  1.   

    加到form的onsubmit上面,如果检查到不是数字,可以防止表单提交
    <form name="form1" onsubmit="return checknum()">
      <p>检查是否有数字:
      <input type="text" name="text3" value="请输入数字" onclick="checktext3()" />
      <input type="button" name="button3" value="检查是否有数字" onclick="checknum()"/>
      </p>
     </form>
      

  2.   

    return false的作用是 检测到第一个不是数字的位置 然后停止循环 帮你节省时间. 其他的不知道你想问什么了....