一、案例效果
分析:
(1)给表单绑定 onsubmit 事件,监听器判断每一个方法的校验结果
(2)定义方法校验每个表单项
(3)给每个表单项绑定 onblur 事件
注意:
(1)<span>
标签被用来组合文档中的行内元素。
(2)处理离焦事件时:
document.getElementById("username").onblur = checkUsername;//是对象而不是checkUsername()
document.getElementById("password").onblur = checkPassword;
(3)获取input 输入框的值:
var userName = document.getElementById("username").value;//正确
var userName = document.getElementById("username").value();错误
(4)提交按钮,点击后总跳动
<td colspan="https://files.jxasp.com/image/2" align="center">
<input type="submit" value="注册" id="btn_sub">
</td>
- 3
原因是设置了 center,可以将其删掉,然后通过 id 为其添加样式即可。
#btn_sub{
margin-top: 8px;
margin-left: 150px;
}
<td colspan="https://files.jxasp.com/image/2">
<input type="submit" value="注册" id="btn_sub">
</td>
- 3
- 4
- 5
- 6
- 7
二、代码
<script>
onload = function () {
// 给表单绑定 onsubmit 事件
document.getElementById("form").onsubmit=function () {
// 校验用户名和密码
return checkUsername() && checkPassword();
}
document.getElementById("username").onblur = checkUsername;
document.getElementById("password").onblur = checkPassword;
}
//校验用户名
function checkUsername() {
var userName = document.getElementById("username").value;
var reg_username = /^\w{6,12}$/;//设置正则规则
var flag = reg_username.test(userName);
var s_username = document.getElementById("s_username");
if (flag){
s_username.innerHTML = "<img width='35' height='25' src='img/gou.png'/>"
}
else {
s_username.innerHTML = "用户名错误!!!"
}
return flag;
}
//校验密码
function checkPassword() {
var Password = document.getElementById("password").value;
var reg_password = /^\w{6,12}$/;//设置正则规则
var flag = reg_password.test(Password);
var s_password = document.getElementById("s_password");
if (flag){
s_password.innerHTML = "<img width='35' height='25' src='img/gou.png'/>"
}
else {
s_password.innerHTML = "密码错误!!!"
}
return flag;
}
</script>
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
<div class="rg_layout">
<div class="rg_left">
<p>新用户注册</p>
<p>USER REGISTER</p>
</div>
<div class="rg_center">
<div class="rg_form">
<form action="#" id="form" method="get" >
<table>
<tr><!--label 标签的作用是当点击文字也会跳到文本输出框-->
<!--for属性与ID属性对应规定 label 绑定到哪个表单元素。-->
<td class="td_left"><label for="username">用户名</label> </td>
<td class="td_right"><input type="text" name="username" id="username">
<span id="s_username" class="error"></span></td>
</tr>
<tr>
<td class="td_left"><label for="password">密码</label> </td>
<td class="td_right"><input type="password" name="password" id="password">
<span id="s_password" class="error"></span></td></td>
</tr>
<tr><!--label 标签的作用是当点击文字也会跳到文本输出框-->
<td class="td_left"><label for="email">email</label> </td>
<td class="td_right"><input type="email" name="email" id="email"> </td>
</tr>
<tr>
<td class="td_left"><label for="name">姓名</label> </td>
<td class="td_right"><input type="text" name="name" id="name"> </td>
</tr>
<tr>
<td class="td_left"><label for="tel">手机号</label> </td>
<td class="td_right"><input type="text" name="tel" id="tel"> </td>
</tr>
<tr>
<td class="td_left">性别</td>
<td class="td_right">
<input type="radio" name="gender" value="male" checked>男
<input type="radio" name="gender" value="female">女
</td>
</tr>
<tr>
<td class="td_left"><label for="birthday">出生日期</label> </td>
<td class="td_right"><input type="date" name="birthday" id="birthday"> </td>
</tr>
<tr>
<td class="td_left"><label for="checkcode">验证码</label> </td>
<td class="td_right">
<input type="text" name="username" id="checkcode">
<img src="img/verify_code.jpg" id="img_check">
</td>
</tr>
<tr>
<td colspan="https://files.jxasp.com/image/2">
<input type="submit" value="注册" id="btn_sub">
</td>
</tr>
</table>
</form>
</div>
</div>
<div class="rg_right">
<p>已有账号?<a href="#">立即登录</a></p>
</div>
</div>
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64