使用javascript实现表单校验
技术分析
确定需要的事件为onfocus()聚焦事件和onblur()离焦事件
在指定的位置输出提示信息,就要innerHTML=‘提示信息’
代码步骤
- 首先确定事件onfocus()聚焦事件,并为其绑定一个函数
- 书写绑定的函数
- 在确定离焦事件onbulr()离焦事件,并未其绑定一个函数
- 书写这个绑定的函数
表单校验代码实现
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#fater{
border:1px solid red;
width:1320px;
height:680px;
margin:auto;
}
#son_one{
border:1px solid blue;
width:1320px;
height:40px;
}
#son_tow{
border:1px solid black;
width:1320px;
height:40px;
}
#son_three{
border:1px solid black;
width:1320px;
height:595px;
margin:auto;
}
.title{
border:1px solid green;
width:438px;
height:40px;
float:left;
}
#son_three_1{
border:1px solid black;
width:500px;
height:500px;
margin-top:50px;
}
#son_one_3{
padding-top:8px;
height:32px;
}
ul li{
/*元素内联*/
display:inline;
color:white;
}
a {
/*去除超链接下划线*/
text-decoration:none;
}
</style>
<script>
//表单校验代码实现
//因为表单有很多文本框需要校验,所以这段代码不是很通用,所以要改进代码。
//聚焦代码时实现
/*function showTips(){
//在指定位置输出内容
document.getElementById("usernamespan").innerHTML="<font color='gray'>用户名必填!</font>";
}*/
//离焦代码实现
/*function showTips2(){
//获取用户名框的值
var username = document.getElementById("username").value;
if(username==""){
document.getElementById("usernamespan").innerHTML="<font color='red'>用户名不能为空!</font>"
}else{
//如果用户名有内容,要把之前用户名必填的内容要清除掉,如果不清除,就会出现用户名有还出现了用户名必填。
document.getElementById("usernamespan").innerHTML=""
}
}*/
//表单校验代码改进
//分析,几乎代码全部都是一样的,就是id和输出的文字的内容不一致。
//聚焦代码改进
function showTips(id,info){
document.getElementById(id+"span").innerHTML="<font color='gray'>"+info+"</font>"
}
//离焦代码改进
function checkText(id,info){
var val = document.getElementById(id).value;
//判断内容是否为空
if(val==""){
//为空
//为空提示填写内容
document.getElementById(id+"span").innerHTML="<font color='red'>"+info+"</font>"
}else{
//不为空
if(id!="emalie"){
//如果文本框不为空,就要把之前的提示要清空。
document.getElementById(id+"span").innerHTML=""
}else{
//这里是为邮箱专门加了是否符合邮箱格式的判断
if(!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(val)){
document.getElementById(id+"span").innerHTML="<font color='red'>邮箱格式不正确!</font>"
}
}
}
}
</script>
</head>
<body>
<div id="fater">
<div id="son_one">
<div class="title">
<img src="../img/logo2.png" width="300px" height="30px"/>
</div>
<div class="title" align="center">
<img src="../img/header.png" width="300px" height="40px" />
</div>
<div id="son_one_3" class="title" align="center">
<a href="#">登录</a>
<a href="#">注册</a>
<a href="#">购物车</a>
</div>
</div>
<div id="son_tow" style="background-color:black;">
<ul>
<a href="#"><li>首页</li></a>
<a href="#"><li>手机数码</li></a>
<a href="#"><li>电脑办公</li></a>
<a href="#"><li>鞋靴箱包</li></a>
<a href="#"><li>家用电器</li></a>
</ul>
</div>
<div id="son_three" style="background-image: url(../img/regist_bg.jpg);" align="center">
<div id="son_three_1" style="background-color:white;">
<table border="1px" width="100%" height="100%" cellspacing="0px">
<tr>
<td colspan="2" height="40px">
<strong>会员注册 USER REGISTER</strong>
</td>
<!--<td width="70%"></td>-->
</tr>
<tr>
<td width="30%" height="40px" align="center">用户名:</td>
<td width="70%" height="40px" align="left">
<input id="username" type="text" onfocus="showTips('username','用户必填!')" onblur="checkText('username','用户名不能为空!')"/>
<span id="usernamespan"></span>
</td>
</tr>
<tr>
<td width="30%" height="40px" align="center">密码:</td>
<td width="70%" height="40px">
<input id="password" type="text" onfocus="showTips('password','密码必填!')" onblur="checkText('password','密码名不能为空!')"/>
<span id="passwordspan"></span>
</td>
</tr>
<tr>
<td width="30%" height="40px" align="center">确认密码:</td>
<td width="70%" height="40px">
<input id="repassword" type="text" onfocus="showTips('repassword','确认密码必填!')" onblur="checkText('repassword','确认密码名不能为空!')"/>
<span id="repasswordspan"></span>
</td>
</tr>
<tr>
<td width="30%" height="40px" align="center">姓名</td>
<td width="70%" height="40px">
<input id="name" type="text" onfocus="showTips('name','姓名必填!')" onblur="checkText('name','姓名不能为空!')"/>
<span id="namespan"></span>
</td>
</tr>
<tr>
<td width="30%" height="40px" align="center">Emalie</td>
<td width="70%" height="40px">
<input id="emalie" type="text" onfocus="showTips('emalie','Emalie必填!')" onblur="checkText('emalie','邮箱地址不能为空!')"/>
<span id="emaliespan"></span>
</td>
</tr>
<tr>
<td width="30%" height="40px" align="center">性别</td>
<td width="70%" height="40px">
<input type="radio" name="sex" checked="checked"/>男
<input type="radio" name="sex"/>女
</td>
</tr>
<tr>
<td width="30%" height="40px" align="center">出生日期</td>
<td width="70%" height="40px">
<input id="birthday" type="text" onfocus="showTips('birthday','Emalie必填!')" onblur="checkText('birthday','出生日期不能为空!')"/>
<span id="birthdayspan"></span>
</td>
</tr>
<tr>
<td width="30%" height="40px" align="center">验证码</td>
<td width="70%" height="40px">
<input id="code" type="text" onfocus="showTips('code','验证码必填!')" onblur="checkText('code','验证码不能为空!')"/>
<span id="codespan"></span>
</td>
</tr>
<tr>
<td width="30%" height="40px" colspan="2" align="center">
<input type="button" value="提交注册"/>
</td>
<!--<td width="70%" height="40px"></td>-->
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
页面效果展示