正则表达式知识点
效果图展示
采用的是正则表达式进行判断的一次性表单验证,点击注册以后才会判断格式是否都正确!
全部代码
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="./css/iconfont.css"/>
<style>
#formuser {
display: block;
margin: 0 auto;
width: 400px;
}
.form_user > li {
list-style: none;
height: 50px;
width: 400px;
border: 1px solid silver;
border-radius: 5px;
position: relative;
margin: 40px 0;
}
.form_user > li.litxt > input {
margin-left: 1px;
width: 378px;
height: 48px;
border-style: none;
outline: none;
font-size: 20px;
color: black;
padding-left: 20px;
}
.tel {
position: relative;
border-style: none !important;
}
.usertelno {
width: 150px;
height: 50px;
border: 1px solid silver;
border-radius: 5px;
line-height: 50px;
position: relative;
float: left;
}
.usertelno > input {
position: absolute;
top: 0;
left: 1px;
width: 120px;
height: 48px;
padding-left: 20px;
border-style: none;
font-size: 20px;
outline: none;
}
#icon {
position: absolute;
right: 10px;
top: 0;
z-index: 2;
color: gray;
}
.ph {
float: right;
width: 210px;
height: 50px;
border-radius: 5px;
border: 1px solid silver;
font-size: 20px;
padding-left: 20px;
outline: none;
}
.btn > input {
width: 400px;
height: 50px;
outline: none;
border-style: none;
background-color: transparent;
color: white;
font-size: 22px;
}
.btn {
background-color: #2e7fff;
}
.btn:hover {
background-color: #40a6ff;
}
.telerror {
clear: left;
display: block;
font-size: 14px;
color: gray;
position: relative;
top: 5px;
}
.emailshow {
display: block;
font-size: 14px;
color: gray !important;
position: absolute;
top: 53px;
}
.error {
font-size: 14px;
color: red;
position: absolute;
top: 53px;
left: 0;
background-image: url("./img/error@2x.png");
background-repeat: no-repeat;
background-size: 20px;
padding-left: 25px;
}
.success {
display: none;
position: absolute;
top: 15px;
right: 10px;
}
</style>
</head>
<body>
<form id="formuser" action="" method="get">
<ul class="form_user">
<li class="litxt">
<input type="text" class="userData" name="email" placeholder="邮箱" value=""/>
<span class="emailshow">请输入你常用的邮箱(非qq/foxmail邮箱)</span>
<img class="success" src="./img/tick.png" alt=""/>
</li>
<li class="litxt">
<input type="text" class="userData" name="nickname" placeholder="昵称" value=""/>
<img class="success" src="./img/tick.png" alt=""/>
</li>
<li class="litxt">
<input type="password" class="userData" name="password" placeholder="密码" value=""/>
<img class="success" src="./img/tick.png" alt=""/>
</li>
<li class="tel">
<div class="usertelno">
<input id="telnum" type="text" value="+86"/>
<i id="icon" class=" iconfont">󰆦</i>
</div>
<input class="ph" placeholder="手机号码" type="text"/>
<span class="telerror">可通过该手机号找回密码</span>
</li>
<li class="btn"><input type="submit" value="立即注册"/></li>
</ul>
</form>
<script>
var emailshow = document.getElementsByClassName("emailshow")[0];
var formuser = document.getElementById("formuser");
var userData = document.getElementsByClassName("userData");
var litxt = document.getElementsByClassName("litxt");
var success = document.getElementsByClassName("success");
var errorinfo = [
{
name: "邮箱",
error: [
{info: "邮箱不能为空!"},
{info: "邮箱格式不正确!"}
]
},
{
name: "昵称",
error: [
{info: "昵称不能为空!"}
]
},
{
name: "密码",
error: [
{info: "密码不能为空!"},
{info: "密码必须包含数字和字母"},
{info: "密码必须在6-8位!"}
]
}
];
formuser.onsubmit = function () {
emailshow.style.display = "none";
for (var i = 0; i < userData.length; i++) {
if (userData[i].value.match(/./) == null) {
addEvent("error", i, 0);
}
else {
//不为0 判断格式
if (i == 0) {
var emailReg = /^[A-Za-z\d]+([-_.][a-zA-Z\d]+)*@([a-zA-Z\d]+[.])+[a-zA-Z\d]{2,4}$/;
if (userData[i].value.match(emailReg)) {
success[i].style.display = "block";
addEvent("success", i, null);
}
else {
addEvent("error", i, 1);
}
}
if (i == 1) {
success[i].style.display = "block";
addEvent("success", i, null);
}
if (i == 2) {
if (userData[i].value.length >= 6 && userData[i].value.length <= 8) {
var pwd=/^([a-zA-Z_]+(\d)+[A-Za-z0-9]*)|((\d)+[a-zA-Z_]+([A-Za-z0-9]*))$/;
//以字母或下划线开头,必须要有数字或 以数字开头 必须有字母或下划线
// var pwd = /^(([a-zA-Z_])+(\d)+([a-zA-Z0-9]*))+|((\d)+([a-zA-Z_])+([a-zA-Z0-9]*))$/;
if (userData[i].value.match(pwd)) {
success[i].style.display = "block";
addEvent("success", i, null);
}
else {
addEvent("error", i, 1);
}
}
else {
addEvent("error", i, 2);
}
}
}
}
var len = document.getElementsByClassName("error").length;
if (len) {
return false;
}
return true;
}
function addEvent(res, index, num) {
if (litxt[index].lastElementChild.className == "error") {
litxt[index].lastElementChild.remove();
}
if (res == "error") {
success[index].style.display = "none";
var span = document.createElement("span");
span.className = "error";
span.innerHTML = errorinfo[index].error[num].info;
litxt[index].appendChild(span);
}
}
</script>
</body>
</html>