how to check confirm password field in form without reloading page

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

    Using Native setCustomValidity

    Compare the password/confirm-password input values on their change event and setCustomValidity accordingly:

    function onChange() {
      const password = document.querySelector('input[name=password]');
      const confirm = document.querySelector('input[name=confirm]');
      if (confirm.value === password.value) {
        confirm.setCustomValidity('');
      } else {
        confirm.setCustomValidity('Passwords do not match');
      }
    }
    <form>
      <label>Password: <input name="password" type="password" onChange="onChange()" /> </label><br />
      <label>Confirm : <input name="confirm"  type="password" onChange="onChange()" /> </label><br />
      <input type="submit" />
    </form>

    0 讨论(0)
  • 2020-12-12 14:35
    function check() {
        if(document.getElementById('password').value ===
                document.getElementById('confirm_password').value) {
            document.getElementById('message').innerHTML = "match";
        } else {
            document.getElementById('message').innerHTML = "no match";
        }
    }
    
    <label>password :
    <input name="password" id="password" type="password" />
    </label>
    <label>confirm password:
    <input type="password" name="confirm_password" id="confirm_password" onchange="check()"/> 
    <span id='message'></span>
    
    0 讨论(0)
  • 2020-12-12 14:35
    $box = $('input[name=showPassword]');
    
    $box.focus(function(){
        if ($(this).is(':checked')) {
            $('input[name=pswd]').attr('type', 'password');    
        } else {
            $('input[name=pswd]').attr('type', 'text');
        }
    })
    
    0 讨论(0)
  • 2020-12-12 14:35

    You can check confirm password by only simple javascript

    html

    <input type="password" name="password" required>
    <input type="password" name="confirmpassword" onkeypress="register()" required>
    <div id="checkconfirm"></div>
    

    and in javascript

       function register() {
    
        var password= document.getElementById('password').value ;
        var confirm= document.getElementById('confirmpassword').value;
    
        if (confirm!=password){
          var field = document.getElementById("checkconfirm")
          field.innerHTML = "not match";
        }
      }
    

    Also you can use onkeyup instead of onkeypress.

    0 讨论(0)
  • 2020-12-12 14:36

    We will be looking at two approaches to achieve this. With and without using jQuery.

    1. Using jQuery

    You need to add a keyup function to both of your password and confirm password fields. The reason being that the text equality should be checked even if the password field changes. Thanks @kdjernigan for pointing that out

    In this way, when you type in the field you will know if the password is same or not:

    $('#password, #confirm_password').on('keyup', function () {
      if ($('#password').val() == $('#confirm_password').val()) {
        $('#message').html('Matching').css('color', 'green');
      } else 
        $('#message').html('Not Matching').css('color', 'red');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label>password :
      <input name="password" id="password" type="password" />
    </label>
    <br>
    <label>confirm password:
      <input type="password" name="confirm_password" id="confirm_password" />
      <span id='message'></span>
    </label>

    and here is the fiddle: http://jsfiddle.net/aelor/F6sEv/325/

    2. Without using jQuery

    We will use the onkeyup event of javascript on both the fields to achieve the same effect.

    var check = function() {
      if (document.getElementById('password').value ==
        document.getElementById('confirm_password').value) {
        document.getElementById('message').style.color = 'green';
        document.getElementById('message').innerHTML = 'matching';
      } else {
        document.getElementById('message').style.color = 'red';
        document.getElementById('message').innerHTML = 'not matching';
      }
    }
    <label>password :
      <input name="password" id="password" type="password" onkeyup='check();' />
    </label>
    <br>
    <label>confirm password:
      <input type="password" name="confirm_password" id="confirm_password"  onkeyup='check();' /> 
      <span id='message'></span>
    </label>

    and here is the fiddle: http://jsfiddle.net/aelor/F6sEv/324/

    0 讨论(0)
  • 2020-12-12 14:37

    If you don't want use jQuery:

    function check_pass() {
        if (document.getElementById('password').value ==
                document.getElementById('confirm_password').value) {
            document.getElementById('submit').disabled = false;
        } else {
            document.getElementById('submit').disabled = true;
        }
    }
    
    <input type="password" name="password" id="password" onchange='check_pass();'/>
    <input type="password" name="confirm_password" id="confirm_password" onchange='check_pass();'/>
    <input type="submit" name="submit"  value="registration"  id="submit" disabled/>
    
    0 讨论(0)
提交回复
热议问题