- js裡面我想同時用onfocus和onbulr事件可是沒有反應,是我寫錯了嗎??
-
<!DOCTYPE html>
<html>
<head>
<meta charset="gbk">
<title>用戶注冊</title>
<script type="text/javascript"src="用戶注冊.js"></script>
</head>
<body>
<form action="" onsubmit="return check();">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="userName" onfocus="showHow();"
onblur="showWrong();" /><span id="userMsg"></span></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password" onfocus="showHow2();"
onblur="showWrong2();" /><span id="passMsg"></span></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="確定"/>
</td>
</tr>
</table>
</form>
</body>
</html>
function showHow() {
var msg = document.getElementById("userMsg");
msg.innerHTML = "首位為字母的4-16位數字、字母、下劃線";
}
function showWrong() {
var msg = document.getElementById("userMsg");
var name = document.getElementById("form").userName;
if (name.value == "") {
msg.innerHTML = "用戶名不能為空";
return false;
}
if (name.value.length < 4) {
msg.innerHTML = "用戶名長度須大於4位";
return false;
}
if (name.value.length > 16) {
msg.innerHTML = "用戶名長度須在16位以內";
return false;
}
var reg = /^[a-zA-z]{1}\w{3,15}$/;
var result = reg.test(name.value);
if (result == false) {
msg.innerHTML = "輸入用戶名格式錯誤";
return false;
}
return true;
}
function showHow2() {
var msg = document.getElementById("passMsg");
msg.innerHTML = "4-10個字母和下劃線";
}
function showWrong2() {
var msg = document.getElementById("passMsg");
var word = document.getElementById("form").password;
var reg = /^[a-zA-Z_]{4,10}$/;
if (word.value == "") {
msg.innerHTML = "密碼不能為空";
return false;
}
if (word.value.length < 4) {
msg.innerHTML = "密碼長度須大於4位";
return false;
}
if (word.value.length > 10) {
msg.innerHTML = "密碼長度須在10位以內";
return false;
}
var result = reg.test(word.value);
if (result == false) {
msg.innerHTML = "輸入密碼格式錯誤";
return false;
}
return true;
}
function check() {
return showWrong() && showWrong2();
}
最佳回答:
function showWrong() {
var msg = document.getElementById("userMsg");
var name = document.getElementById("form").userName;//表單id忘記加了
<form action="" onsubmit="return check();" id="form">