how to check confirm password field in form without reloading page

后端 未结 15 1798
青春惊慌失措
青春惊慌失措 2020-12-12 13:58

I have a project in which I have to add a registration form and I want to to validate that the password and confirm fields are equal without clicking the register button.

相关标签:
15条回答
  • 2020-12-12 14:37

    I think this example is good to check https://codepen.io/diegoleme/pen/surIK

    I can quote code here

    <form class="pure-form">
        <fieldset>
            <legend>Confirm password with HTML5</legend>
    
            <input type="password" placeholder="Password" id="password" required>
            <input type="password" placeholder="Confirm Password" id="confirm_password" required>
    
            <button type="submit" class="pure-button pure-button-primary">Confirm</button>
        </fieldset>
    </form>
    

    and

    var password = document.getElementById("password")
      , confirm_password = document.getElementById("confirm_password");
    
    function validatePassword(){
      if(password.value != confirm_password.value) {
        confirm_password.setCustomValidity("Passwords Don't Match");
      } else {
        confirm_password.setCustomValidity('');
      }
    }
    
    password.onchange = validatePassword;
    confirm_password.onkeyup = validatePassword;
    
    0 讨论(0)
  • 2020-12-12 14:38

    Try this one ;

    CSS

    #indicator{
        width:20px;
        height:20px;
        display:block;
        border-radius:10px;
    }
    .green{
        background-color:green; 
        display:block;
    }
    .red{
        background-color:red;   
        display:block;
    }
    

    HTML

    <form id="form" name="form" method="post" action="registration.php"> 
        <label >username : 
        <input name="username" id="username" type="text" /></label> <br>
        <label >password : 
        <input name="password" id="password" type="password" id="password" /></label>      <br>
        <label>confirm password:
        <input type="password" name="confirm_password" id="confirm_password" /><span id="indicator"></span> <br>
        </label>
        <label>
        <input type="submit" name="submit" id="regbtn"  value="registration"  />
        </label>
    </form>
    

    JQuery

    $('#confirm_password').keyup(function(){
        var pass    =   $('#password').val();
        var cpass   =   $('#confirm_password').val();
        if(pass!=cpass){
            $('#indicator').attr({class:'red'});
            $('#regbtn').attr({disabled:true});
        }
        else{
            $('#indicator').attr({class:'green'});
            $('#regbtn').attr({disabled:false});
        }
    });
    
    0 讨论(0)
  • 2020-12-12 14:39

    WITHOUT clicking the button you will have to listen to the change event of the input fields

    var confirmField = document.getElementById("confirm_password");
    var passwordField = document.getElementById("password");
    
    function checkPasswordMatch(){
        var status = document.getElementById("password_status");
        var submit = document.getElementById("submit");
    
        status.innerHTML = "";
        submit.removeAttribute("disabled");
    
        if(confirmField.value === "")
            return;
    
        if(passwordField.value === confirmField.value)
            return;
    
        status.innerHTML = "Passwords don't match";
        submit.setAttribute("disabled", "disabled");
    }
    
    passWordField.addEventListener("change", function(event){
        checkPasswordMatch();
    });
    confirmField.addEventListener("change", function(event){
        checkPasswordMatch();
    });
    

    then add the status element to your html:

    <p id="password_status"></p>
    

    and set the submit button id to submit

    ... id="submit" />
    

    hope this helps you

    0 讨论(0)
提交回复
热议问题