<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>登录验证</title>
</head>
<body>
<h1>登录验证</h1>
<b id="stuLoginMess" style="color: red;"></b>
<form action="login.html" method="post">
<table border="1" cellpadding="0" width="20%">
<tr>
<td>用户名:</td>
<td><input type="text" id="username" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" id="password" /></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" id="code" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" id="submit" value="提交" />
<input type="reset" id="reset" value="重置" />
</td>
</tr>
</table>
</form>
<script>
window.onload = function() {
var fm = document.getElementsByTagName('form')[0];
var stuLoginMess = document.getElementById('stuLoginMess');
var reset = document.getElementById('reset');
reset.onclick = function() {
username.value = ''; // 清空
password.value = ''; // 清空
code.value = ''; // 清空
username.focus(); // 将焦点移动到表单字段
};
fm.onsubmit = function() {
if(!/^\w{6,20}$/.test(username.value)) {
stuLoginMess.innerHTML = "* 用户名错误!";
username.value = ""; // 清空
username.focus(); // 将焦点移动到表单字段
return false;
}
if(!/^\w{6,20}$/.test(password.value)) {
stuLoginMess.innerHTML = "* 密码错误!";
password.value = ''; // 清空
password.focus(); // 将焦点移动到表单字段
return false;
}
if(!/^\d{4}$/.test(code.value)) {
stuLoginMess.innerHTML = "* 验证码错误!";
code.value = ''; // 清空
code.focus(); // 将焦点移动到表单字段
return false;
}
return true;
};
};
</script>
</body>
</html>