请问,各项在未输入内容时如何点击提交实现一键验证,然后这个程序应该如何优化?非常感谢
来源:4-8 编程练习
学到沉迷
2019-05-11 15:08:56
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin: 0;padding: 0; } #registeredBox{ width: 800px;height: 500px;margin: 100px auto; background: #ccc; } h3{ background: blue; width:100%; textIndent: 2em; margin-bottom: 40px; } #inf{ height: 300px; width: 500px; margin: 50px auto; clear: both; } label{ display: block; float: right; font-size: 14px; } select,label>input{ width: 150px; height: 20px; margin-top:15px; margin-left: 10px; margin-right: 20px; } select{ height: 25px; text-align: center; margin-right: 220px; } label>span{ display: inline-block; width: 200px; font-size: 12px; color:#f00; } #submitBtn{ width:60px; height: 30px; font-size: 14px; font-weight: bold; color:#fff; border:none; border-radius: 5px; background: blue; margin-left: 370px; } </style> </head> <body> <div id="registeredBox"> <h3>用户注册</h3> <div id="inf"> <label>用户名:<input type="text" id="username"><span id="usernameInfo"></span></label> <label>登录密码:<input type="text" id="password"><span id="passwordInfo"></span></label> <label>确认密码:<input type="text" id="repeatPs"><span id="repeatPsInfo"></span></label> <label>姓名:<input type="text" id="name"><span id="nameInfo"></span></label> <label>性别: <select> <option value="male">男</option> <option value="female">女</option> </select> </label> <label>身份证号码:<input type="text" id="idCard"><span id="idCardInfo"></span></label> <label>邮箱:<input type="text" id="email"><span id="emailInfo"></span></label> <label>手机号码:<input type="text" id="phone"><span id="phoneInfo"></span></label> </div> <input type="submit" id="submitBtn" value="提交"> </div> <script> var submitBtn = document.getElementById('submitBtn'); //正则 var regExp={ username:/^[a-z]\w{5,19}$/i, password:/^\S{6,18}$/, name:/^[\u4e00-\u9fa5]{2,4}$/, idCard:/^\d{15}|\d{17}x|\d{18}$/, email:/^\w+@[a-z]+\.com(?:\.[a-z])?/i, phone:/^(13)[0-9]{9}|(147)[0-9]{8}|(15)[0,1,2,3,5,6,7,8,9]{1}[0-9]{8}|(18)[0,2,5,6,7,8,9]{1}[0-9]{8}$/ } // 各项输入框 var inputs={ username:document.getElementById('username'), password:document.getElementById('password'), repeatPs:document.getElementById('repeatPs'), name:document.getElementById('name'), idCard:document.getElementById('idCard'), email:document.getElementById('email'), phone:document.getElementById('phone') } // 各项提示内容 var infos={ usernameInfo:document.getElementById('usernameInfo'), passwordInfo:document.getElementById('passwordInfo'), repeatPsInfo:document.getElementById('repeatPsInfo'), nameInfo:document.getElementById('nameInfo'), idCardInfo:document.getElementById('idCardInfo'), emailInfo:document.getElementById('emailInfo'), phoneInfo:document.getElementById('phoneInfo') } // 检测两次密码是否一致 inputs.repeatPs.onblur=function(){ if(inputs.password.value||passwordInfo.innerHTML=='OK'){ if(inputs.password.value==inputs.repeatPs.value&&inputs.repeatPs.value!=null){ repeatPsInfo.innerHTML='OK'; repeatPsInfo.style.color = 'green'; }else{ repeatPsInfo.innerHTML='两次密码不一致'; inputs.repeatPs.focus(); } }else{ repeatPsInfo.innerHTML='请先设置密码'; } } // 验证各项输入准确性 function checkForm(inputsName,regexp,infos,errors){ inputsName.onblur=function(){ infos.innerHTML=''; if(regexp.test(inputsName.value)){ infos.innerHTML='OK' infos.style.color = 'green'; }else{ infos.innerHTML=errors; infos.style.color = 'red'; } } } // 点击提交判断验证情况 submitBtn.onclick=function(){ var num=0,count=0; for(var i in infos){ num++; if(infos[i].innerHTML=='OK'){ count++; } } if(count==num){ alert('验证已通过!'); }else{ alert('验证未成功!'); } } checkForm(inputs.username,regExp.username,infos.usernameInfo,'6-20位字母、数字或“_”,字母开头'); checkForm(inputs.password,regExp.password,infos.passwordInfo,'数字字母或符号,中间不能有空格'); checkForm(inputs.name,regExp.name,infos.nameInfo,'真实姓名,两位到四位的中文汉字'); checkForm(inputs.idCard,regExp.idCard,infos.idCardInfo,'请输入18位的身份证号码'); checkForm(inputs.email,regExp.email,infos.emailInfo,'邮箱格式不正确'); checkForm(inputs.phone,regExp.phone,infos.phoneInfo,'手机号码不正确'); </script> </body> </html>
3回答
邮箱的验证不严谨,建议参考:
希望可以帮到你!
学到沉迷
提问者
2019-05-11
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin: 0;padding: 0; } #registeredBox{ width: 800px;height: 500px;margin: 100px auto; background: #ccc; } h3{ background: blue; width:100%; textIndent: 2em; margin-bottom: 40px; } #inf{ height: 300px; width: 500px; margin: 50px auto; clear: both; } label{ display: block; float: right; font-size: 14px; } select,label>input{ width: 150px; height: 20px; margin-top:15px; margin-left: 10px; margin-right: 20px; } select{ height: 25px; text-align: center; margin-right: 220px; } label>span{ display: inline-block; width: 200px; font-size: 12px; color:#f00; } #submitBtn{ width:60px; height: 30px; font-size: 14px; font-weight: bold; color:#fff; border:none; border-radius: 5px; background: blue; margin-left: 370px; } </style> </head> <body> <div id="registeredBox"> <h3>用户注册</h3> <div id="inf"> <label>用户名:<input type="text" id="username"><span id="usernameInfo"></span></label> <label>登录密码:<input type="text" id="password"><span id="passwordInfo"></span></label> <label>确认密码:<input type="text" id="repeatPs"><span id="repeatPsInfo"></span></label> <label>姓名:<input type="text" id="name"><span id="nameInfo"></span></label> <label>性别: <select> <option value="male">男</option> <option value="female">女</option> </select> </label> <label>身份证号码:<input type="text" id="idCard"><span id="idCardInfo"></span></label> <label>邮箱:<input type="text" id="email"><span id="emailInfo"></span></label> <label>手机号码:<input type="text" id="phone"><span id="phoneInfo"></span></label> </div> <button type="submit" id="submitBtn"> 提交</button> </div> <script> var submitBtn = document.getElementById('submitBtn'); //正则 var regExp={ username:/^[a-z]\w{5,19}$/i, password:/^\S{6,18}$/, name:/^[\u4e00-\u9fa5]{2,4}$/, idCard:/^\d{15}|\d{17}x|\d{18}$/, email:/^\w+@[a-z]+\.com(?:\.[a-z])?/i, phone:/^(13)[0-9]{9}|(147)[0-9]{8}|(15)[0,1,2,3,5,6,7,8,9]{1}[0-9]{8}|(18)[0,2,5,6,7,8,9]{1}[0-9]{8}$/ } // 各项输入框 var inputs={ username:document.getElementById('username'), password:document.getElementById('password'), repeatPs:document.getElementById('repeatPs'), name:document.getElementById('name'), idCard:document.getElementById('idCard'), email:document.getElementById('email'), phone:document.getElementById('phone') } // 各项提示内容 var infos={ usernameInfo:document.getElementById('usernameInfo'), passwordInfo:document.getElementById('passwordInfo'), repeatPsInfo:document.getElementById('repeatPsInfo'), nameInfo:document.getElementById('nameInfo'), idCardInfo:document.getElementById('idCardInfo'), emailInfo:document.getElementById('emailInfo'), phoneInfo:document.getElementById('phoneInfo') } // 检测两次密码是否一致 inputs.repeatPs.onblur=function(){ if(inputs.password.value||passwordInfo.innerHTML=='OK'){ if(inputs.password.value==inputs.repeatPs.value&&inputs.repeatPs.value!=null){ repeatPsInfo.innerHTML='OK'; repeatPsInfo.style.color = 'green'; }else{ repeatPsInfo.innerHTML='两次密码不一致'; inputs.repeatPs.focus(); } }else{ repeatPsInfo.innerHTML='请先设置密码'; } } // 验证各项输入准确性 function checkForm(inputsName,regexp,infos,errors){ inputsName.onblur=function(){ infos.innerHTML=''; if(regexp.test(inputsName.value)){ infos.innerHTML='OK' infos.style.color = 'green'; }else{ infos.innerHTML=errors; infos.style.color = 'red'; } } } // 点击提交判断验证情况 submitBtn.onclick=function(){ var num=0,count=0; var inputList=document.getElementsByTagName('input'); for(var j=0;j<inputList.length;j++){ inputList[j].onblur(); } for(var i in infos){ num++; if(infos[i].innerHTML=='OK'){ count++; } } if(count==num){ alert('验证已通过!'); }else{ alert('验证未成功!'); } } checkForm(inputs.username,regExp.username,infos.usernameInfo,'6-20位字母、数字或“_”,字母开头'); checkForm(inputs.password,regExp.password,infos.passwordInfo,'数字字母或符号,中间不能有空格'); checkForm(inputs.name,regExp.name,infos.nameInfo,'真实姓名,两位到四位的中文汉字'); checkForm(inputs.idCard,regExp.idCard,infos.idCardInfo,'请输入18位的身份证号码'); checkForm(inputs.email,regExp.email,infos.emailInfo,'邮箱格式不正确'); checkForm(inputs.phone,regExp.phone,infos.phoneInfo,'手机号码不正确'); </script> </body> </html> 修改后
学到沉迷
提问者
2019-05-11
点击提交实现一键验证会了,请问整体上该如何优化呢?
相似问题