javascript密码匹配,如果不匹配显示在innerHTML

javascript password match if not match show in innerHTML

本文关键字:不匹配 显示 innerHTML 如果不 如果 密码 javascript      更新时间:2023-09-26

你好,你写的代码是对我有帮助,但我想在innerHTML显示消息时密码不匹配如何做到这一点,我正在尝试,但不为我工作。下面是我的代码,请指教。我是初学者。

                if (pwd != cpwd) {
                 document.getElementById("pwd").innerHTML="password must be match";
     document.getElementById("cpwd").innerHTML="password must be match";
     document.getElementById("pwd").style.color="RED";
      return false;

}

我想知道如何准确地写在innerHTML

在代码下面

               <input id="pass1" type="password" placeholder="Password" style="border-radius:7px; border:2px solid #dadada;" /> <br />
               <input id="pass2" type="password" placeholder="Confirm Password" style="border-radius:7px; border:2px solid #dadada;"/> <br />
 <script>
function myFunction() {
    var pass1 = document.getElementById("pass1").value;
    var pass2 = document.getElementById("pass2").value;
    if (pass1 != pass2) {
        //alert("Passwords Do not match");
        document.getElementById("pass1").style.borderColor = "#E34234";
        document.getElementById("pass2").style.borderColor = "#E34234";
    }
    else {
        alert("Passwords Match!!!");
    }
}

Sumbit

谢谢提前

您应该使用绑定到字段"pass2"的事件onBlur,以便触发附加到您的问题的第一个代码片段。

例如:

document.getElementById("pass2").onblur=function(){
    var pass1 = document.getElementById("pass1").value;
    var pass2 = document.getElementById("pass2").value;
    if (pass1 != pass2) {
      document.getElementById("pass1").innerHTML="password must be match";
      document.getElementById("pass2").innerHTML="password must be match";
      document.getElementById("pass1").style.color="RED";
      return false;
     }
     return true;    
};

添加一个div来存储对文档(如<div id='validate'></div>)的密码验证响应,然后在检查了密码匹配之后,可以在该div的html中显示相应的结果document.getElementById('validate').innerHTML="passwords do not match!";