0

I've found some similar questions. However I'm still having trouble fixing this. I am having troubles with my confirm password validation. If I write something in the password input and then write the same password in my cpassword input, it validates the cpassword. However if I delete a letter from my password, the cpassword still thinks it got it right.

This is my cpassword-check.js

// Validate confirm password while typing
$(document).ready(function() {    
    $('#cpassword').click(function(){  
        var cpassword = $('#cpassword').val(); 
        if (cpassword.length > 0) {  
            $("#resultcpass").html('<i class="fa fa-circle-o-notch fa-spin loading-icon"></i>');
            $.ajax({
                type : 'POST',
                url  : 'cpassword-check.php',
                data : {
                    cpassword: $('#cpassword').val(), 
                    password: $('#password').val()
                }, 
                success : function(data) {
                    $("#resultcpass").html(data);
                }
            });
            return false;

        } else {
            $("#resultcpass").html('');
        }
    });
});

And this is where it validates it (cpassword-check.php):

/*$host="localhost";
$user="root";
$pass="";
$dbname="lr";

$dbcon = new PDO("mysql:host={$host};dbname={$dbname}",$user,$pass);*/

if($_POST['cpassword']) {
    $cpassword = strip_tags($_POST['cpassword']);
    $password = strip_tags($_POST['password']);

    if ( $cpassword != $password) {
        echo "<i class='fa fa-circle user-validation-w pull-right' aria-hidden='true'></i><span class='availability pull-right'> Deben coincidir</span>";
    } else  {
        echo "<i class='fa fa-check user-validation-ok pull-right' aria-hidden='true'></i><span class='availability pull-right'></span>";
    }
}

Would you please help me to execute that function every second or maybe do something else to validate the cpassword even if the password changes?

Thank you!

3
  • Why are you validating it with an AJAX call? You can do it in plain JavaScript, and you've to do it in PHP again anyway because you are able to remove the AJAX request in the most browsers. Commented Dec 27, 2016 at 8:12
  • is it mandatory that the password check has to be done in php or can it be done in javascript itself? Commented Dec 27, 2016 at 8:12
  • It's not mandatory, I just did it that way because I saw an example and I'm not very good at jscript. How can I validate it with plain js? Commented Dec 27, 2016 at 8:21

3 Answers 3

2

Simply use Jquery for it.

$(document).ready(function() {
  $("#register").on('click', function() {
    password = $.trim($("#password").val());
    repassword = $.trim($("#repassword").val());
    var msg = "";
    if (password != repassword) {
      msg += " <p>Password did not match</p>";
    }
    if (msg == "") {
      $("#register_form").submit();
    } else {
      $("#msg").html(msg); // show error message
    }
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution! but I think i'll stick to the ajax call. I know that might be a waste of time but it's working hahaha. Thanks anyways!
1

You have applied validation only on click event of cpassword field, however the same should be applied to both as

// Validate confirm password while typing

$(document).ready(function()
{    
 $('#cpassword').click(function(){
  validatePass();
});

 $('#password').click(function(){
  validatePass();
});

function validate(){
   var cpassword = $('#cpassword').val(); 

    if (cpassword.length > 0)
  {  
    $("#resultcpass").html('<i class="fa fa-circle-o-notch fa-spin loading-icon"></i>');


          /*$.post("email-check.php", $("#reg-form").serialize())
           .done(function(data){
            $("#resultmail").html(data);
           });*/

 $.ajax({

type : 'POST',
url  : 'cpassword-check.php',
data : {

    cpassword: $('#cpassword').val(), 
    password: $('#password').val()

    }, 

success : function(data)
    {
          $("#resultcpass").html(data);
       }
});
return false;

  }
  else
  {
  $("#resultcpass").html('');
  }

 });
}

5 Comments

i'll give it a try.
Using the change event on the inputs gives a much nicer effect, you're able to use the "submit" function on the parent form as well @DiegoRios
@DevNiels would you mind giving me an example? I don't have much experience with jquery/javascript.
@DiegoRios he is trying to say is that instead of click event you should use change event as $("your-field").change(function(){})
Ohh, so everytime the field changes the function is executed. I'll try that. Thanks!
0

Bind function to check password on keypress event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.