请老师帮忙检查一下代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表单验证</title>
</head>
<body>
<div class="box">
<p><b>*</b>用 户 名:
<input type="text" placeholder="用户名设置成功后不可修改" id="nameinp">
<span class="asxd" id="asxd">6-30位字母、数字或_,字母开头</span>
</p>
</div>
<div class="box enter">
<p><b>*</b>登入密码:
<input type="password" placeholder='6-20位字母或数字' id="passinp">
<span id="redbox" class="box2"></span>
<span id="yellowbox" class="box2"></span>
<span id="greenbox" class="box2"></span><br>
<span class='tishi' id="tishi">6-20位字母或数字</span>
</p>
</div>
<div class="box">
<p><b>*</b>确认密码:
<input type="password" placeholder='再次输入您的登入密码' id="yeinp">
<span id="tishi1"></span>
</p>
</div>
<div class="box1">
<button id="button">注册</button>
</div>
</body>
</html>
<style>
.box {
height: 50px;
}
.box input {
width: 220px;
height: 30px;
border-radius: 5px;
border: 1px solid #ccc;
}
.box b {
color: red;
}
.box1 {
margin-top: 60px;
margin-left: 160px;
}
.box1 button {
width: 230px;
height: 40px;
background-color: orange;
color: white;
text-align: center;
line-height: 40px;
font-size: 18px;
border: none;
border-radius: 5px;
}
.box .asxd {
color: rgba(255, 166, 0, 0.795);
margin-left: 15px;
}
.box .tishi {
font-size: 12px;
color: red;
margin-left: 100px;
display: none;
}
.enter {
width: 600px;
position: relative;
}
.enter .box2 {
display: inline-block;
width: 50px;
height: 7px;
background-color: #ccc;
margin-left: 10px;
}
</style>
<script>
var nameinp = document.getElementById('nameinp');
var asxd = document.getElementById('asxd');
var passinp = document.getElementById('passinp')
var yeinp = document.getElementById('yeinp')
var tishi = document.getElementById('tishi')
var redbox = document.getElementById('redbox')
var yellowbox = document.getElementById('yellowbox')
var greenbox = document.getElementById('greenbox')
var tishi1 = document.getElementById('tishi1')
var button= document.getElementById('button')
// 用户名离开事件
nameinp.onblur = function () {
var xinxi = nameinp.value;
if (/^[a-zA-Z]\w{5,29}$/.test(xinxi)) {
asxd.innerText = '用户名输入正确'
asxd.style.color = 'green';
} else {
asxd.innerText = '6-30位字母、数字或_,字母开头'
asxd.style.color = 'red';
}
}
// 登入密码事件
passinp.onblur = function () {
yellowbox.style.backgroundColor = '#ccc';
redbox.style.backgroundColor = '#ccc';
greenbox.style.backgroundColor = '#ccc';
tishi.style.display = 'none';
var xinxi = passinp.value;
if (/^[a-zA-Z0-9]{6,20}$/.test(xinxi)) {
if (/^[0-9]+$|^[a-z]+$|^[A-Z]+$/.test(xinxi)) {
redbox.style.backgroundColor = 'red';
} else if (/^[a-z0-9]+$|^[A-Z0-9]+$|^[a-zA-Z]+$/.test(xinxi)) {
yellowbox.style.backgroundColor = 'yellow';
redbox.style.backgroundColor = 'red';
} else if (/^[a-z0-9A-Z]+$/.test(xinxi)) {
yellowbox.style.backgroundColor = 'yellow';
redbox.style.backgroundColor = 'red';
greenbox.style.backgroundColor = 'green';
}
} else {
tishi.style.display = 'inline';
}
}
// 密码确认框
yeinp.onblur = function () {
var xinxi = yeinp.value;
if (xinxi) {
if (passinp.value == xinxi) {
tishi1.innerText = '两次输入一致';
tishi1.style.color = 'green';
} else {
tishi1.innerText = '两次密码输入不一致,请重新输入';
tishi1.style.color = 'red';
}
} else {
tishi1.innerText = '输入框不能为空';
tishi1.style.color = 'red';
}
}
button.onclick=function(){
if(/^[a-zA-Z]\w{5,29}$/.test(nameinp.value)&&passinp.value == yeinp.value){
alert('信息填写正确');
}else{
alert('请填写信息正确');
}
}
</script>
16
收起
正在回答 回答被采纳积分+1
2回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星