1

i want to show alert and return false when the password and confirm password doesnt same or when the password is empty.

here what i try.

<div class="card margin-card-password">
    <div class="card-header">Change Password</div>
        <div class="card-body">
        <form id="change_password" name="change_password" method="get" action="action/save.php">
            <div class="col-sm-6">
            <div class="form-group">
            <label for="New Password">New Password</label>
            <input type="password" class="form-control" id="password" name="password" value="">
            </div>
            <div class="form-group">
            <label for="Confirm Password">Confirm Password</label>
            <input type="password" class="form-control" id="confirm_password" value="">
            </div>
            </div>
            <div class="col-sm-10">
            <button type="submit" class="btn btn-primary" id="change_password" name="change_password">Save</button>
            </div>
        </form>
    </div>
</div>

js

<script type="text/javascript">
   $('#change_password').on('click', function(){
       var password = $.trim($("#password").val());
       var confirmpassword = $.trim($("#confirm_password").val());
       if(password != confirmpassword) || (password.includes ("")){
           alert('The Password is Doesnt Same!');
           return false;
        }
    });
</script>
Jazuly
  • 1,374
  • 5
  • 20
  • 43

2 Answers2

3

password.includes ("") This part of code checks if password contains "" - empty string. Every string contains empty string. You need to check if string is an empty string like this - password == "".

Hope this will help You

  $('#change_password').on('click', function(){
       var password = $.trim($("#password").val());
       var confirmpassword = $.trim($("#confirm_password").val());
       if(password != confirmpassword || password == ""){
           alert('The Password is Doesnt Same!');
           return false;
       }
    });

And this

$('#change_password').on('click', function(){
    var password = $.trim($("#password").val());
    var confirmpassword = $.trim($("#confirm_password").val());
    if(password != confirmpassword){
        alert('The Password is Doesnt Same!');
        return false;
    }else if(password == ""){
        alert('The Password is empty!');
        return false;
    }
});

P.S. Sorry for bad English

Yura Rosiak
  • 255
  • 2
  • 13
1

As your password variable is already assigned as the input value, change password.includes("") to the following:

password.length == 0

As a note, I'd recommend that your alert should read as 'Incorrect password' for easier readability.

DKyleo
  • 806
  • 8
  • 11