2013-06-19 28 views
0

嗨,你写的代码,这对我很有帮助,但我想在innerHTML中显示消息时密码不匹配如何做到这一点我正在尝试但不为我工作。下面是我的代码。请引导我。我是初学者。javascript密码如果不匹配显示在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!!!"); 
    } 
} 

透过

感谢提前 等待您的欣赏答案。

+0

[Your code seems to work](http://jsfiddle.net/UA9q8/)。什么是'cpwd'和'pwd'?你有什么问题? –

+0

你想放哪里..你的“pwd”和“cpwd”元素在哪里 – user2587222

回答

0

添加一个div来存储你的密码验证响应像<div id='validate'></div>文档,然后选择已经检查密码匹配,则可以显示在这个HTML相应的结果后的div document.getElementById('validate').innerHTML="passwords do not match!";

1

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

例如:

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;  
}; 

另一种选择是把它绑提交按钮。