Javascript验证密码

时间:2015-08-22 22:58:01

标签: javascript

我是java-script的新手我现在正在学习php但我在HTML表单中验证密码时遇到问题 这是我的代码:

show

任何人都可以帮我修复它!

3 个答案:

答案 0 :(得分:0)

更改以下内容:

var pass = document.getElementById(pass);
var conf_pass = document.getElementById(conf_pass);

人:

var pass = document.getElementById('password');
var conf_pass = document.getElementById('confirm_password');

答案 1 :(得分:0)

当您致电dpcument.getElementById()时,请确保传入字符串作为参数ID。对于pass,请为第一个"password"传递<input>,为第二个"confirm_password"传递<input>

&#13;
&#13;
//Pass in strings into document.getElementById():
var pass = document.getElementById("password");
var conf_pass = document.getElementById("confirm_password");

function ValidatePass() {
    if (pass.value != conf_pass.value) {
        document.getElementById("pass_state").innerHTML = "Password Does not match"
    }else {
        document.getElementById("pass_state").innerHTML = "Password match"
    }
}

//Now, if we bind the ValidatePass function to onkeypress event of the pass and conf_pass, we will see that the submit button will run ValidatePass correctly!
pass.addEventListener("keypress", ValidatePass);
conf_pass.addEventListener("keypress", ValidatePass);
&#13;
<form action="add_user.php" method="post">
    <h3>Name</h3>
    <input name="name" placeholder="Name">
    <h3>Username</h3>
    <input name="username" placeholder="Username">
    <h3>E-mail</h3>
    <input name="email" placeholder="E-mail">
    <h3>Password</h3>
    <input name="password" placeholder="Name" id="password" type="password">
    <h3>Confirm Password</h3>
    <input name="confirm_pass" placeholder="Name" id="confirm_password" type="password">
    <p id="pass_state"></p>
    <input type="submit" value="Create User">
</form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

永远不会调用ValidatePass()。不确定你的代码是不完整的,但是我没有看到ID为“pass”的任何内容,所以获取一个id不存在的元素是没用的。