Compare fields in jQuery

Asked

Viewed 551 times

0

I need to compare the fields according to the script below:

    $("#send_request").submit(function(event){
        var password  = '7c4a8d09ca3762af61e59520943dc26494f8941b'; 
        if($('#userpassword').val()!=password){
            alert('senha invalida');
        }
      event.preventDefault();
    });

Only that the password of userpassword of this field, will come without encryption, so I will have to encrypt before comparing. But the form passes directly, nor compares and does not give anything. How should I do?

  • Don’t send passwords to the client side! Even if you do var pwd = <?= pd ?>; this will be accessible by the user. Also a hashed encrypted password is never equal to a password entered in a field input type="password". You have to use ajax, send it to PHP and compare it there.

1 answer

0


Without seeing the html code of the form gets a little complicated, but its js code seems to me to be ok, I managed to make it run here without any more problems, without taking into account that it is not encrypting anything yet. My form made like this, look if you agree:

<form id="send_request" action="#" method="POST">
  <input id="userpassword" type="text">
  <button type="submit">Enviar</button>
</form>  
$("#send_request").submit(function(event){  
  var password  = '7c4a8d09ca3762af61e59520943dc26494f8941b'; 
  if($('#userpassword').val()!=password){
    alert('senha invalida');
  }
  event.preventDefault();
});
  • Theoretically that’s it, but at the time of clicking the button, it goes into the form action... and needed to encrypt the userpassword, in php. ,.. I encrypt it so oh <?=$this->Encrypt->sha1($accounts_details->security_password);? > how do I put it there in jquery?

  • Well, you can call your own page in php via AJAX to get the SHA1 password, or use some javascript library to do it at runtime.

  • Another approach is to let the form submit, and on your php page do the processing and checking, if the password entered is not equal, go back to the form page and show some message to the user.

  • Yes, I’ve done it this way... but can I make it back and with the form filled? I’m thinking about registering them in cookies

  • Yes. (: even in session you can put this data too.

  • Remembering that the ideal, as Sérgio commented upstairs, is not to send passwords on the client side, it is dangerous. I just tried to answer your question according to your question, but we have to be careful when handling user data. Hug!

  • Yes, I handled the issue by PHP, and I made the return if the password typed is not his, or incorrect, however, in return the fields come empty in case of error, but I will save the POST in Sesssion or cookies, and fill in the fields in case of error

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.