在登录注册时,为了我们账户的安全,往往需要进行邮箱格式、手机号格式、密码的一致性进行验证,才能进行注册登录。
下面是手机号,邮箱,密码等的正则表达式:
> 手机号正则:/^(?:(?:\+|100)86)?1[3-9]\d{9}$/
> 或者:/^( ? (?:\+|100)86)?1\d{10}$/
账号正则:
/^[A-Za-z0-9]+$/
邮箱正则:
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
密码强度正则:最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符 。
/^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/
知道了这些简单的正则表达式,我们才能更好的实现登录注册功能
下面是登录注册验证账号、密码、手机号等的格式的图:
登录注册的HTML代码:
<div class="register">
<form action="#" name="regform" method="post">
<p>
用户注册
</p>
<!--
用户名-->
<div class="username">
<div class="pp">
<label>用 户 名</label>
<input name="user" id="txt-name" placeholder="你的账户名与登录名" />
</div>
</div>
<div id="Username">
<span id="tip-name">*用户名为8-12字母或数字(不能使用数字开头)</span>
</div>
<!--密码-->
<div class="password">
<div class="pp">
<label>设置密码</label>
<input name="pass" id="txt-pwd" placeholder="建议使用两种字符组合" />
</div>
</div>
<div id="Password">
<span id="tip-pwd">*密码为6位数字</span>
</div>
<!-- 确定密码-->
<div class="password-determine">
<div class="pp">
<label>确定密码</label>
<input name="rpass" id="txt-pwd1" placeholder="请再次输入密码" />
</div>
</div>
<div id="Rpass">
<span id="tip-pwd1">*两次输入的密码不一致</span>
</div>
<!--手机号-->
<div class="photo">
<div class="pp">
<label>
<select >
<option>中国 +86</option>
</select>
</label>
<input name="rphoto" id="txt-tel" maxlength="11" placeholder="请输入手机号" οnblur="photo()" /><span></span>
</div>
</div>
<div id="p4rphoto">
<span id="tip-tel">*手机号码不正确</span><br>
</div>
<!--邮箱验证-->
<div class="email">
<p>或
<a href="">邮箱验证</a>
</p>
</div>
<!-- 手机验证-->
<div class="verification-code">
<div class="pp">
<label>
手机验证码
</label>
<input name="p-verification" placeholder="请输入验证码" />
<button style="background-color:Azure;">获取验证码</button>
</div>
</div>
<!-- 按钮-->
<div class="btn">
<input type="checkbox" id="cbx">
<p>同意<a href="">《xxxxx注册协议》</p>
</a>
</div>
<div class="submit">
<input type="submit" value="立即注册" class="register-immediately" disabled id="btn-sub" οnclick="return filter()">
</div>
</form>
</div>
CSS代码:
body {
font-size: 18px;
font-family: "微软雅黑";
background: url(../img/timg.jpg) no-repeat top center;
color: #FFF;
}
form,
p {
padding: 0;
margin: 0;
border: 0;
}
span {
color: red;
font-size: 5px;
display: none;
}
form {
width: 550px;
height: 450px;
padding-top: 10px;
margin: 100px auto;
/*使表单在浏览器中居中*/
background: rgba(255, 255, 255, 0.1);
/*为表单添加背景颜色*/
border-radius: 20px;
border: 1px solid rgba(255, 255, 255, 0.3);
}
input {
width: 130px;
height: 30px;
padding: 0 auto;
margin-left: -50px;
}
p {
margin-top: 6px;
text-align: center;
}
.pp label {
float: left;
padding-left: 10px;
}
.pp input {
border: none;
outline: none;
background: none;
color: rgb(204, 204, 204);
}
.pp select {
border: none;
outline: none;
background: none;
font-size: 16px;
margin-left: -5px;
color: #FFFFFF;
/*color: rgb(204, 204, 204);*/
}
.username {
width: 310px;
line-height: 40px;
margin-left: 120px;
margin-top: 20px;
text-align: center;
border-radius: 5px;
display: inline-block;
border: 1px solid rgba(255, 255, 255, 0.5);
}
#Username {
width: 113px;
height: 35px;
float: right;
margin-top: 25px;
/*background-color: red;*/
}
.password {
width: 310px;
line-height: 40px;
margin-left: 120px;
margin-top: 20px;
text-align: center;
border-radius: 5px;
display: inline-block;
border: 1px solid rgba(255, 255, 255, 0.5);
}
#Password {
width: 113px;
height: 35px;
float: right;
margin-top: 25px;
/*background-color: red;*/
}
.password-determine {
width: 310px;
line-height: 40px;
margin-left: 120px;
margin-top: 20px;
text-align: center;
border-radius: 5px;
display: inline-block;
border: 1px solid rgba(255, 255, 255, 0.5);
}
#Rpass {
width: 113px;
height: 35px;
float: right;
margin-top: 25px;
/*background-color: red;*/
}
.photo {
width: 310px;
line-height: 40px;
margin-left: 120px;
margin-top: 20px;
text-align: center;
border-radius: 5px;
display: inline-block;
border: 1px solid rgba(255, 255, 255, 0.5);
}
#p4rphoto {
width: 113px;
height: 35px;
float: right;
margin-top: 25px;
/*background-color: red;*/
}
.email {
width: 100px;
margin-left: 350px;
/*display: inline-block;*/
position: absolute;
}
.email p {
margin: 0 auto;
font-size: 10px;
}
.email a {
text-decoration: none;
}
.verification-code {
width: 310px;
line-height: 40px;
margin-left: 120px;
margin-top: 20px;
text-align: center;
border-radius: 5px;
border: 1px solid rgba(255, 255, 255, 0.5);
}
.verification-code button {
width: 85px;
height: 35px;
float: right;
border-radius: 5px;
margin-top: 2px;
position: absolute;
font-family: charcoal;
border: none;
}
.verification-code input {
width: 90px;
}
.p-registered {
width: 310px;
height: 40px;
background-color: red;
margin-left: 120px;
margin-top: 10px;
border-radius: 5px;
border: 1px solid rgba(255, 255, 255, 0.5);
}
.Sign-up-now p a {
text-decoration: none;
color: beige;
}
.btn {
width: 210px;
font-size: 12px;
height: 20px;
}
#cbx {
height: 15px;
margin-left: 63px;
}
.btn p {
float: left;
width: 180px;
margin-left: 115px;
margin-top: -20px;
}
.btn a {
text-decoration: none;
}
.submit input {
width: 310px;
height: 40px;
font-size: 18px;
color: #F5F5DC;
background-color: red;
margin-left: 120px;
margin-top: 10px;
border-radius: 5px;
border: 1px solid rgba(255, 255, 255, 0.5);
}
JS验证代码:
<script>
var cbx = document.getElementById('cbx'); //协议
var btn = document.getElementById('btn-sub');
var txtName = document.getElementById('txt-name');
var tipName = document.getElementById('tip-name');
var txtPwd = document.getElementById('txt-pwd');
var tipPwd = document.getElementById('tip-pwd');
var txtPwd1 = document.getElementById('txt-pwd1');
var tipPwd1 = document.getElementById('tip-pwd1');
var txtPhone = document.getElementById('txt-tel');
var tipPhone = document.getElementById('tip-tel');
setCbx(cbx); //页面加载时,根据复选框状态设置按钮状态
//复选框改变事件
cbx.onchange = function() {
setCbx(this);
};
//设置复选框
function setCbx(obj) {
//判断,若为勾中状态,则提交按钮可用
if (obj.checked) {
btn.className = 'two';
btn.disabled = false;
} else {
btn.className = 'one';
btn.disabled = true;
}
}
//校验表单数据
function filter() {
return filterUserName() && filterPwd() && filterTel();
//分别校验 用户名、密码、手机号
window.open("index.html")
}
//校验用户名
function filterUserName() {
var sName = txtName.value;
var p = /^[a-zA-Z]\w{7,11}$/;
//校验不通过
if (!p.test(sName)) {
tipName.style.display = 'inline-block';
txtName.focus(); //获取焦点
return false;
}
return true;
}
//校验密码
function filterPwd() {
var pwd = txtPwd.value; //读取输入框值
var p = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
var pwd1 = txtPwd1.value;
//校验密码
if (!p.test(pwd)) {
tipPwd.style.display = 'inline-block';
txtPwd.focus();
return false;
}
//两次密码是否一致
if (pwd != pwd1) {
tipPwd1.style.display = 'inline-block';
txtPwd1.focus();
return false;
}
return true;
}
//校验手机号
function filterTel() {
var tel = txtPhone.value;
var p = /^( ? (?:\+|100)86)?1\d{10}$/;
if (!p.test(tel)) {
tipPhone.style.display = 'inline-block';
txtPhone.focus();
return false;
}
return true;
}
</script>