版权声明:转载请标明出处 https://blog.csdn.net/Ms_yjk/article/details/84337573
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>检测两次输入的密码是否一致</title>
<script>
function CheckPassword() {
var pswd1 = document.getElementById("password");
var pswd2 = document.getElementById("again_password");
if(pswd1.value != pswd2.value) {
//调试使用 alert("密码不一致");
document.getElementById("span_Check").style.display = "inline";
return false;
}
else
{
document.getElementById("span_Check").style.display = "none";
//调试使用 alert("Pass!");
}
}
</script>
</head>
<body>
<label for="password"> 密 码 : </label>
<input type="password" class="form-control" id="password">
<br />
<label for="password">确认密码 :</label>
<input type="password" class="form-control" id="again_password">
<br />
<span id="span_Check" class="span_Check" style="display: none;font-size: 10px;color: red;font-family: '微软雅黑';"> 两次填写的密码不一致 !</span>
<br />
<button type="button" onclick="CheckPassword()">Register</button>
</body>
</html>
通过id 以及 document.getElementById("span_Check").style.display 找到要改变的文本的样式 (display属性)
CheckPassword方法判断两次密码的值是否一致,并返回Boolean值。