用js检测文本框中输入的是否符合条件并有错误和正确提醒

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>捕获异常</title>
	</head>
    <script type="text/javascript">
		function my_func(){
			
			try{
				x=document.getElementById("input_id").value;
				alert(x);
				if(x==""||x==null) throw "非空";
				if(isNaN(x)) throw "必须是数字";
				if(x<5) throw "数字太小";
				if(x>10) throw "数字太大";
				else throw "输入正确";
			}catch(err){
				document.getElementById("demo").innerHTML="提示:"+err;
			}
	    }
	</script>
	
	<body>
		<p>请输入5——10之间的数字</p>
		<input type="text" id="input_id"> </input>
		<button type="button" id="button_id" onclick="my_func()">确定</button>
		<p id = "demo"></p>		
	</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/zxrxzw/p/10792589.html
今日推荐