<body>
<p>请输入1~10的数字:</p>
<input id="input" type="text" onfocus="clearAll(this)">
<input type="button" value="校验" onclick = "check()">
<p id="msg"></p>
</body>
<script>
function check(){
try{
var x = document.getElementById("input").value;
if(x==""){throw "值为空!";}
if(isNaN(x)){throw "不是数字!";}
if(x<1){throw "数字太小!";}
if(x>10){throw "数字太大!";}
}
catch(error){
var y = document.getElementById("msg");
y.innerHTML = "错误:" + error ;
}
}
function clearAll(x){
x.value="";
}
</script>
本文介绍了一个简单的数字输入验证脚本,该脚本通过JavaScript实现,能够确保用户输入的是1到10之间的数字,并提供了即时反馈。
2145

被折叠的 条评论
为什么被折叠?



