正则表达式表单验证

<!DOCTYPE html>

<html lang="en">

 

<head>

<meta charset="UTF-8">

<title>注册页面</title>

 

 

<!-- register css文件 -->

<link rel="stylesheet" href="css/register(1).css">

<script src="js/注册页表单验证.js"></script>

</head>

 

<body>

 

<!-- registerarea -->

<div class="registerarea">

<h3>

注册新用户

<em>

                    我有账号,去<a href="login.html" data-toggle="modal" data-target=".login">登陆</a>

                </em>

</h3>

<div class="reg_form">

 

<form action="demo.php">

<ul>

<li >

<label for="tel">手机号:</label>

<input type="text" class="inp" id="tel">

<span class="">     

                        

                        </span>

</li>

<li>

<label for="">QQ:</label>

<input type="text" class="inp" id="qq">

<span></span>

</li>

<li>

<label for="">昵称:</label>

<input type="text" class="inp" id="nc">

<span></span>

</li>

<li id="one-li">

<label for="">短信验证码:</label>

<input type="text" class="inp" id="msg">

<span></span>

<button id="btn">获取验证码</button>

</li>

<li>

<label for="">登陆密码:</label>

<input type="text" class="inp" id="pwd">

<span>

</span>

</li>

<li class="safe">

安全程度

<em >弱</em>

<em >中</em>

<em >强</em>

</li>

 

<li>

<label for="">确认密码:</label>

<input type="text" class="inp" id="surepwd">

<span></span>

</li>

<li class="agree">

 

<input type="checkbox">同意协议并注册

<a href="#">《知果果用户协议》</a>

</li>

<li>

<input type="submit" value="完成注册" class="over">

</li>

</ul>

</form>

 

</div>

</div>

</body>

 

</html>

 

 

 

<---JS文件--->

window.οnlοad= function(){

var regtel = /^1[3|4|5|7|8]\d{9}$/

var regqq = /^[1-9]\d{4,}$/; // 10000

var regnc = /^[\u4e00-\u9fa5]{2,8}$/;

var regmsg = /^\d{6}$/;

var regpwd = /^[a-zA-Z0-9_-]{6,16}$/;

 

//密码弱

var regpwdR = /^[0-9]{6,16}$/;

//密码中

var regpwdZ = /^[a-zA-Z0-9]{6,16}$/;

//密码强

var regpwdQ = /^[a-zA-Z0-9_-]{6,16}$/;

 

var tel = document.querySelector('#tel');

var qq = document.querySelector('#qq');

var nc = document.querySelector('#nc');

var msg = document.querySelector('#msg');

var pwd = document.querySelector('#pwd');

var surepwd = document.querySelector('#surepwd');

var safe = document.querySelector('.safe')

 

regExp(qq,regqq);

regExp(nc,regnc);

regExp(msg,regmsg);

regExp(pwd,regpwd);


 

// 封装一个函数

function regExp(ele,reg){

ele.onblur = function(){

if ( this.value.trim() != ''){

if(reg.test(this.value)){

// alert(11);

this.nextElementSibling.className = 'success';

this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您格式输入正确';

} else {

// alert(22);

this.nextElementSibling.className = 'error';

this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 格式不正确,请从新输入 ';

}

} else {

this.nextElementSibling.innerHTML = '';

}

}

}

 

regExp1(tel,regtel);

regExp1(pwd,regpwd);

//封装一个手机号和密码表单实时监控函数

function regExp1(ele1,reg1){

ele1.addEventListener("input",function(){

if ( this.value.trim() != ''){

if(reg1.test(this.value)){

// alert(11);

this.nextElementSibling.className = 'success';

this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您格式输入正确';

} else {

// alert(22);

this.nextElementSibling.className = 'error';

this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 格式不正确,请从新输入 ';

}

} else {

this.nextElementSibling.innerHTML = '';

}

})

}

 

//密码框和确认密码框监控表单内容

pwd.addEventListener('input',function(){

if(this.value.trim() != ''){

if(regpwd.test(this.value)){

if(regpwdR.test(this.value)){

for ( var i = 0; i < this.parentNode.nextElementSibling.children.length; i ++){

this.parentNode.nextElementSibling.children[i].className = '';

this.parentNode.nextElementSibling.children[0].className = 'ruo';

}

// safe.children[0].className = "ruo";

} else if (regpwdZ.test(this.value)) {

for ( var i = 0; i < this.parentNode.nextElementSibling.children.length; i ++){

this.parentNode.nextElementSibling.children[i].className = '';

this.parentNode.nextElementSibling.children[1].className = 'zhong';

}

} else if (regpwdQ.test(this.value)) {

for ( var i = 0; i < this.parentNode.nextElementSibling.children.length; i ++){

this.parentNode.nextElementSibling.children[i].className = '';

this.parentNode.nextElementSibling.children[2].className = 'zhong';

}

}

}

} else if(pwd.value == ''){

surepwd.nextElementSibling.innerHTML = '';

for ( var i = 0; i < this.parentNode.nextElementSibling.children.length; i ++){

this.parentNode.nextElementSibling.children[i].className = '';

}

}

})


 

//确认密码框

surepwd.addEventListener("input",function(){

if( pwd.value.trim() != ''){

if(this.value.trim().length > 0){

// alert(33)

if(pwd.value === this.value){

this.nextElementSibling.className = 'success';

this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您格式输入正确';

} else {

// alert(22);

this.nextElementSibling.className = 'error';

this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 两次输入不一样,请从新输入 ';

}

} else {

// alert('请输入内容');

this.nextElementSibling.className = 'error';

this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 请输入内容 ';

}

} else {

this.nextElementSibling.innerHTML = '';

}

})


 

//获取验证码

var btn = document.querySelector('button');

var time = 10; // 定义剩下的秒数

btn.addEventListener('click', function() {

btn.disabled = true;

var timer = setInterval(function() {

if (time == 0) {

// 清除定时器和复原按钮

clearInterval(timer);

btn.disabled = false;

btn.innerHTML = '重新获取';

time = 3;

} else {

btn.innerHTML = '还剩下' + time + '秒';

time--;

}

}, 1000);

 

})

}

 

 

 

<-----CSS文件------>

.w {

width: 1200px;

margin: auto;

}

 

.header {

height: 82px;

border-bottom: 2px solid #b1191a;

}

 

.logo {

padding-top: 15px;

}

 

.registerarea {

height: 580px;

border: 1px solid #ccc;

margin-top: 20px;

}

 

.registerarea h3 {

height: 40px;

border-bottom: 1px solid #ccc;

background-color: #ececec;

padding: 0 10px;

font-weight: 400;

line-height: 40px;

font-size: 18px;

}

 

.registerarea h3 em {

float: right;

font-size: 14px;

}

 

.registerarea a {

color: #c81623;

}

 

.reg_form {

width: 600px;

height: 400px;

margin: 40px auto 0;

}

 

.reg_form li {

margin-bottom: 15px;

}

 

.reg_form label {

display: inline-block;

width: 100px;

height: 36px;

line-height: 36px;

text-align: right;

}

.reg_form .one-li {

position: relative;

}

.btn {

position: absolute !important ;

top: 100px !important;

left: 100px !important;

}

.inp {

width: 238px;

height: 34px;

border: 1px solid #ccc;

margin-left: 10px;

}

 

.error {

color: #df3033;

margin-left: 10px;

}

 

.error_icon,

.success_icon {

display: inline-block;

width: 20px;

height: 20px;

background: url(../img/error.png) no-repeat;

vertical-align: middle;

margin-top: -2px;

}

 

.success {

color: #40b83f;

margin-left: 10px;

}

 

.success_icon {

background-image: url(../img/success.png);

}

 

.safe {

padding-left: 187px;

color: #b2b2b2;

}

 

.safe em {

padding: 0 12px;

color: #fff;

background-color: #ccc;

}

 

.safe .ruo {

background-color: #de1111 ;

}

 

.safe .zhong {

background-color: #40b83f;

}

 

.safe .qiang {

background-color: #f79100;

}

 

.agree {

padding-top: 20px;

padding-left: 100px;

}

 

.agree input {

vertical-align: middle;

margin-right: 5px;

}

 

.agree a {

color: #1ba1e6;

}

 

.over {

width: 200px;

height: 34px;

background-color: #c81623;

margin: 30px 0 0 130px;

border: none;

color: #fff;

font-size: 14px;

}

 

.footer {

height: 120px;

text-align: center;

}

 

.links {

margin-top: 20px;

height: 30px;

}

 

.copyright {

line-height: 20px;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值