Python-100-Days/Day21-30/code/demo01/form.html

178 lines
4.9 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
font-size: 18px;
}
#login label {
display: inline-block;
width: 150px;
text-align: right;
margin-right: 20px;
}
.formitem {
margin: 20px 0;
}
.hint {
display: inline-block;
width: 320px;
font-size: 14px;
margin-left: 10px;
}
.correct {
color: green;
}
.incorrect {
color: red;
}
#login input[type="submit"] {
display: inline-block;
width: 120px;
height: 30px;
background-color: darkred;
color: white;
font-size: 20px;
line-height: 30px;
border: none;
cursor: pointer;
margin-left: 200px;
}
</style>
</head>
<body>
<form id="login" action="" method="post">
<div class="formitem">
<label for="username">用户名: </label>
<input type="text" id="username" name="username">
<span id="uidHint" class="hint"></span>
</div>
<div class="formitem">
<label for="password">密码: </label>
<input type="password" id="password" name="password">
<span id="pwdHint" class="hint"></span>
</div>
<div class="formitem">
<label for="repassword">确认密码: </label>
<input type="password" id="repassword">
<span id="rePwdHint" class="hint"></span>
</div>
<div class="formitem">
<label for="tel">手机号: </label>
<input type="text" id="tel" name="tel">
<span id="telHint" class="hint"></span>
</div>
<div class="formitem">
<label for="code">验证码: </label>
<input type="text" id="code" name="code" size="4">
<input type="button" value="获取验证码">
</div>
<div class="formitem">
<input type="submit" value="立即开通">
</div>
<div class="formitem">
<label for="agreement"></label>
<input type="checkbox" id="agreement">
<span class="hint">我同意<a href="#">《XYZ服务协议》</a></span>
</div>
</form>
<script src="js/mylib.js" ></script>
<script>
(function() {
// 使用正则表达式的字面量语法创建正则表达式对象
var uidRegEx = /^\w{6,20}$/;
var pwdRegEx = /^.{8,20}$/;
var telRegEx = /^1[345789]\d{9}$/;
var uid = $('username');
function checkUsername() {
var uidHint = $('uidHint');
var username = uid.value.trim();
if (uidRegEx.test(username)) {
uidHint.textContent = '√';
uidHint.className = 'hint correct';
return true;
} else {
uidHint.textContent = '用户名由字母数字下划线构成且长度为6-20个字符';
uidHint.className = 'hint incorrect';
return false;
}
}
handleEvent(uid, 'blur', checkUsername);
var pwd = $('password');
function checkPassword() {
var pwdHint = $('pwdHint');
var password = pwd.value;
if (pwdRegEx.test(password)) {
pwdHint.textContent = '√';
pwdHint.className = 'hint correct';
return true;
} else {
pwdHint.textContent = '密码长度为8-20个字符';
pwdHint.className = 'hint incorrect';
return false;
}
}
handleEvent(pwd, 'blur', checkPassword);
var rePwd = $('repassword');
function checkRepassword() {
var rePwdHint = $('rePwdHint');
var password = pwd.value;
var repassword = rePwd.value;
if (repassword.length == 0) {
rePwdHint.textContent = '确认密码不能为空';
rePwdHint.className = 'hint incorrect';
return false;
}
if (repassword == password) {
rePwdHint.textContent = '√';
rePwdHint.className = 'hint correct';
return true;
} else {
rePwdHint.textContent = '密码和确认密码不一致';
rePwdHint.className = 'hint incorrect';
return false;
}
}
handleEvent(rePwd, 'blur', checkRepassword);
var telInput = $('tel');
function checkTel() {
var telHint = $('telHint');
var tel = telInput.value;
if (telRegEx.test(tel)) {
telHint.textContent = '√';
telHint.className = 'hint correct';
return true;
} else {
telHint.textContent = '请输入有效的手机号';
telHint.className = 'hint incorrect';
return false;
}
}
handleEvent(telInput, 'blur', checkTel);
var form = $('login') || document.forms[0];
// 给表单对象绑定表单提交事件
handleEvent(form, 'submit', function(evt) {
evt = evt || window.event;
// 阻止表单提交等到验证通过了之后手动提交表单
evt.preventDefault();
if (!$('agreement').checked) {
alert('请先选中同意《XYZ服务协议》');
return ;
}
// 请注意&&和&之间区别 前者有短路效果后者没有
if (checkUsername() & checkPassword() &
checkRepassword() & checkTel()) {
var target = evt.target || evt.srcElement;
// 如果所有表单数据验证都通过了就提交表单
target.submit();
}
});
}());
</script>
</body>
</html>