how to check confirm password field in form without reloading page

后端 未结 15 1801
青春惊慌失措
青春惊慌失措 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

    Confirm password with HTML5

    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;
    

提交回复
热议问题