形式验证多个输入的js

时间:2017-01-05 19:35:56

标签: javascript html forms

我想以几种形式检查密码输入。我没有收到任何错误消息,但我不知道为什么它不起作用。任何ideads如何解决它?感谢

Link: link

1 个答案:

答案 0 :(得分:0)

js代码中几乎没有更正:请参阅下面的代码:

  1. this.passwordInputs.lengththis.value.length
  2. var pgMsg = document.querySelector(".form-container p");var pgMsg = document.querySelector(".form-element p");
  3. var error = "";
    var passwordInputs = document.querySelectorAll('input[type=password]');
    
    if (passwordInputs.length > 1) {
        for (var i = 0; i < passwordInputs.length; i++) {
            var passwordInputsList = passwordInputs[i];
            passwordInputsList.addEventListener("blur", passwordCheck, false);
        }
    }
    
    function passwordCheck() {
        if (this.value.length < 8) {
            error = "Password is too short";
            var pgMsg = document.querySelector(".form-element p");
            pgMsg.textContent = error;
            console.log(pgMsg.textContent);
        }
    }
    <div class="form-element">
        <label for="register-password"></label>
        <img src="img/Icons/password.png" alt="Password Icon">
        <input type="password" name="password" id="register-password" placeholder="Password">
        <p class="pg-valid" id="pg-valid"></p>
    </div>
    
    <div class="form-element">
        <label for="register-password-repeat"></label>
        <img src="img/Icons/password.png" alt="Password Icon">
        <input type="password" name="password-repeat" id="register-password-repeat" placeholder="Confirm Password">
    </div>
    
    <div class="form-element">
        <label for="signin-password"></label>
        <img src="img/Icons/password.png" alt="Password Icon">
        <input type="password" id="signin-password" name="password" placeholder="Password">
    </div>