用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>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM