js表单验证密码(确认密码)
- 案例需求:在注册时,输入密码对其进行密码强度验证,并判断两次输入的密码是否一致。例如,密码长度至少8位,并且英文与数字组合。
- 代码实现
<p>输入密码:<input type="password" id="pwd1" name="pwd1" onblur="isPwd()" /><br/>
<span id="pwd1Span" style="color: red;font-family: 楷体"></span></p>
<p>确认密码:<input type="password" id="pwd2" name="pwd2" onblur="isCom()" /><br/>
<span id="pwd2Span" style="color: red;font-family: 楷体"></span></p>
<script>
//密码校验
function isPwd(){
var text= document.getElementById("pwd1").value;
var re =/^(?=.*[a-z])(?=.*\d)[^]{8,16}$/;
var result = re.test(text);
if(!result) {
document.getElementById("pwd1Span").innerHTML = "密码必须包含数字,字母,且不少于8位";
return false;
}else {
document.getElementById("pwd1Span").innerHTML = "";
return true;
}
};
//确认密码
function isCom(){
var text1= document.getElementById("pwd1").value;
var text2= document.getElementById("pwd2").value;
if (text2 == text1){
document.getElementById("pwd2Span").innerHTML = "";
return true;
}else {
document.getElementById("pwd2Span").innerHTML = "两次输入的密码不一致";
return false;
}
}
</script>
3.效果图