1. 程式人生 > >正則表示式——驗證密碼輸入6-8位字母和數字(失去焦點觸發)

正則表示式——驗證密碼輸入6-8位字母和數字(失去焦點觸發)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>失去焦點觸發事件</title>
		<script>
		 	function myfunction(){
				var x = document.getElementById("input1");
				c =  /^[a-zA-Z0-9]{6,8}$/;				
				if((x.value).match(c)){
					alert("輸入正確!");
					w.style.display="none";
				}
				else if((x.value)==""){
					alert("請輸入!");
				}
				else{
					alert("錯誤!");
					x.value = "";
				}
			}
		</script>
		<script>
			function ttt(){
				var w = document.getElementById("p1");
				w.style.display="block";
			}
		</script>
	</head>
	<body>
		<input type="text" id="input1" onblur="myfunction()" onclick="ttt()" />
		<p id="p1" style="display: none;color: red;">請輸入6-8位數字和字母的組合!</p>
	</body>	
</html>