简体   繁体   English

检查密码是否与jquery匹配

[英]check if passwords match with jquery

i am trying to validate the two inputs to match each-other with jquery and i couldn't find a way to do it. 我试图验证两个输入以匹配每个其他与jquery,我找不到方法来做到这一点。 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. 我无法使jquery获取两个输入的值,因此无法检查它们是否相互匹配。 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' 输入是用codeignly进行的,所以对于不熟悉它的人来说 - 第一个输入的id是'pass'而第二个是'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!');
        }

    });
});

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!');
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM