<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>14-案例-表单校验</title>
<style type="text/css">
.regist_bg {
width: 100%;
height: 600px;
padding-top: 40px;
background-image: url(../img/bg.jpg);
}
.regist {
border: 7px inset #ccc;
width: 700px;
padding: 40px 0;
padding-left: 80px;
background-color: #fff;
margin-left: 25%;
border-radius: 10px;
}
input[type="submit"] {
background-color: aliceblue;
width: 100px;
height: 35px;
color: red;
cursor: pointer;
border-radius: 5px;
}
.warn{
color:red;
font-size: 12px;
display: none;
}
</style>
<!--
表单校验
1. 两次密码输入一致
2. 邮箱格式正确
3. 手机号格式正确
4. 提交表单时校验表单项是否合法.
总结:
form表单的 onsubmit 事件 表单提交之前触发,用法实例:
onsubmit="return 函数()" 函数返回true则表单正常提交,函数返回false则阻止表单提交
-->
</head>
<body>
<div class="regist_bg">
<div class="regist">
<form action="#" onsubmit="javascript:return jundgeSubmit()">
<table width="700px" height="350px">
<tr>
<td colspan="3">
<font color="#3164af">会员注册</font> USER REGISTER
</td>
</tr>
<tr>
<td align="right">用户名</td>
<td colspan="2"><input id="loginnameId" type="text" name="loginname" size="50" /></td>
</tr>
<tr>
<td align="right">密码</td>
<td colspan="2"><input id="loginpwdId" type="password" name="loginpwd" size="50" /> </td>
</tr>
<tr>
<td align="right">确认密码</td>
<td colspan="2"><input id="reloginpwdId" type="password" name="reloginpwd" size="50" /><span id="pwdwarn" class="warn">密码不一致</span> </td>
</tr>
<tr>
<td align="right">Email</td>
<td colspan="2"><input id="emailId" type="text" name="email" size="50" /> <span id="emailwarn" class="warn">邮箱格式有误</span> </td>
</tr>
<tr>
<td align="right">姓名</td>
<td colspan="2"><input name="text" name="username" size="50" /> </td>
</tr>
<tr>
<td align="right">性别</td>
<td colspan="2">
<input type="radio" name="gender" value="男" checked="checked" />男
<input type="radio" name="gender" value="女" />女
</td>
</tr>
<tr>
<td align="right">电话号码</td>
<td colspan="2"><input type="text" name="phone" size="50" /> </td>
</tr>
<tr>
<td align="right">所在地</td>
<td colspan="3">
<select id="provinceId" onchange="selectCity(this.value)" style="width:150px">
<option value="">----请-选-择-省----</option>
<option value="0">北京</option>
<option value="1">辽宁省</option>
<option value="2">江苏省</option>
</select>
<select id="cityId" style="width:150px">
<option>----请-选-择-市----</option>
</select>
</td>
</tr>
<tr>
<td width="80" align="right">验证码</td>
<td width="100"><input type="text" name="verifyCode" /> </td>
<td><img src="../img/checkMa.png" /> </td>
</tr>
<tr>
<td></td>
<td colspan="2">
<input id="rebtn" type="button" value="注册" />
</td>
</tr>
</table>
</form>
</div>
</div>
<script >
// 1. 两次密码输入一致
//获取原密码值
let loginpwd = document.querySelector("#loginpwdId");//密码输入框对象
//获取确认密码值
let reloginpwd = document.querySelector("#reloginpwdId");//密码输入框对象
function pwd(){
//比较两个值,如果不相等提示警告
let pwdwarn = document.querySelector("#pwdwarn");//密码警告
if (loginpwd.value == reloginpwd.value) {
pwdwarn.style.display = "none";//不显示警告
return true;//校验通过
}else{
pwdwarn.style.display = "inline";//不显示警告
return false
}
}
reloginpwd.onchange = pwd ;//确认密码值改变时进行校验
// 2. 邮箱格式正确
//获取邮箱输入框元素
let email = document.querySelector("#emailId");//获取邮箱输入框元素
let emailwarn = document.querySelector("#emailwarn");//邮箱警告元素
//判断邮箱格式是否正确,格式有误提示警告
function judgeEmail(){
let emailRegexp = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
let boo = emailRegexp.test(email.value);//判断邮箱格式
if (boo) {
emailwarn.style.display = "none";//不显示警告
return true;//校验通过
}else{
emailwarn.style.display = "inline";//不显示警告
return false
}
}
email.onchange = judgeEmail;//邮箱地址值改变时进行校验
// 3. 手机号格式正确
let phoneRegexp = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
//4. 提交表单时校验表单项是否合法.
function jundgeSubmit(){
console.log("执行表单校验");
let boo = pwd() && judgeEmail() ;//所有校验 全部合法才提交表单
console.log(boo);
return boo;//为true允许提交表单, 为false阻止表单提交
}
</script>
</body>
</html>
你好鸭达可
- 粉丝: 416
- 资源: 5
最新资源
- vlmcsd-1113-2020-03-28-Hotbird64(最新版本KMS)
- 433.基于SpringBoot的冷链物流系统(含报告).zip
- com.harmonyos4.exception.PowerFailureException(怎么解决).md
- 使用 Python 字典统计字符串中每个字符的出现次数.docx
- com.harmonyos4.exception.SystemBootFailureException(怎么解决).md
- 球队获胜数据集.zip
- ERR-NULL-POINTER(解决方案).md
- <项目代码>YOLOv8 航拍行人识别<目标检测>
- 计算机网络-socket-inet-master.zip
- Java编程学习路线:从基础到实战全攻略
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈