Comparing 2 password fields if they are equal

0

I have a password field and confirm password, I want to check if both are the same.

NOTE: There is an autocomplete OFF for the email field

$(document).ready(function () {
    $('#txtEmail').attr('autocomplete','off');

    if( $('#txtSenha').val() != $('#txtSenhaConfirme').val()   )
    {
        alert('Senhas diferentes');
        return false;
    }
});

No JS error, just does not work. NOTE: Even if you have corrected the typo, it has not yet been validated.

    
asked by anonymous 01.12.2014 / 13:04

3 answers

5

You have some errors in your code, syntax invalid (missing $( in #txtSenhaConfirme ).

Anyway, I doubt if you want to do this check directly here $(document).ready(function () { because that basically means "when the page has just loaded".

To check two values you can use .value or .val() depending on whether you are using pure JavaScript or jQuery.

So, and probably within a function, or an event such as submit, you can do so:

if($('#txtSenha').val() != $('#txtSenhaConfirme').val()){
    alert('Senhas diferentes');
    return false;
}

Example:

$('form').on('submit', function () {
    if ($('#txtSenha').val() != $('#txtSenhaConfirme').val()) {
        alert('Senhas diferentes');
        return false;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formaction="">
    <input type="text" id="txtSenha" />
    <input type="text" id="txtSenhaConfirme" />
    <input type="submit" />
</form>
    
01.12.2014 / 13:15
2

The error is happening here:

$('#txtSenha').val() != '#txtSenhaConfirme').val()   

Forgetting a $( while the correct one would be:

$('#txtSenha').val() != $('#txtSenhaConfirme').val()   
    
01.12.2014 / 13:15
0

I've had problems with some browsers (IE7, IE6) with .val() . It's worth trying $('#txtSenha').text() != '#txtSenhaConfirme').text()

    
01.12.2014 / 17:35