最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - check if passwords match with jquery - Stack Overflow

programmeradmin5浏览0评论

i am trying to validate the two inputs to match each-other with jquery and i couldn't find a way to do it. i can't make the jquery to get the values of the two inputs, therefore it can't check if they are not matching eachother. the input are made with codeignither, so for anyone who doesn't familiar with it - the id of the first input is 'pass' and the second is 'pass2'

<tr>
            <td>choose a password:</td>
            <td><?= form_password('password', '', 'placeholder="choose a password" required="required" id="pass"') ?></td>
        </tr>
        <tr>
            <td>re-type the password:</td>
            <td><?= form_password('password2', '', 'placeholder="re-type your password" required="required" id="pass2"') ?></td>
        </tr>

the script -

$(document).ready(function(){
    var pass = $('#pass').val();
    var pass2 = $('#pass2').val();

    $('#pass2').focusout(function(){
        if(pass != pass2){
            alert('the passwords didn\'t match!');
        }

    });
});

i am trying to validate the two inputs to match each-other with jquery and i couldn't find a way to do it. i can't make the jquery to get the values of the two inputs, therefore it can't check if they are not matching eachother. the input are made with codeignither, so for anyone who doesn't familiar with it - the id of the first input is 'pass' and the second is 'pass2'

<tr>
            <td>choose a password:</td>
            <td><?= form_password('password', '', 'placeholder="choose a password" required="required" id="pass"') ?></td>
        </tr>
        <tr>
            <td>re-type the password:</td>
            <td><?= form_password('password2', '', 'placeholder="re-type your password" required="required" id="pass2"') ?></td>
        </tr>

the script -

$(document).ready(function(){
    var pass = $('#pass').val();
    var pass2 = $('#pass2').val();

    $('#pass2').focusout(function(){
        if(pass != pass2){
            alert('the passwords didn\'t match!');
        }

    });
});
Share Improve this question asked Oct 16, 2014 at 18:23 kshaykkshayk 831 silver badge8 bronze badges 1
  • Look at the HTML, not the … whatever that template language is. – Quentin Commented Oct 16, 2014 at 18:25
Add a ment  | 

2 Answers 2

Reset to default 12

because you are reading the values on document ready, not when the user changed them. Move it inside the focusout.

$(document).ready(function(){
    $('#pass2').focusout(function(){
        var pass = $('#pass').val();
        var pass2 = $('#pass2').val();
        if(pass != pass2){
            alert('the passwords didn\'t match!');
        }

    });
});

Because you need to write the code as

$('#pass2').focusout(function(){
    // get values on the focusout event
    var pass = $('#pass').val();
    var pass2 = $('#pass2').val();
    // test them...
    if(pass != pass2){
        alert('the passwords didn\'t match!');
    }

});
发布评论

评论列表(0)

  1. 暂无评论