【问题标题】:Jquery checking that passwords match with php + JsonJquery 检查密码是否与 php + Json 匹配
【发布时间】:2010-03-09 10:35:37
【问题描述】:

我有一个使用 JS 和 PHP 验证的表单。除了我尝试检查密码是否匹配之外,一切都进展顺利。

这是表格:

<div>
                    <label for="passlength">Password, valid: 0-9</label>
                    <input type="text" name="passlength" value="<?=@$_REQUEST['passlength']?>" id="passlength" />
                    <span id="validatePasslength"><?php if ($error) { echo $error['msg']; } ?></span>
                </div>
                <div>
                    <label for="passlength">Password2, valid: 0-9</label>
                    <input type="text" name="passlength2" value="<?=@$_REQUEST['passlength2']?>" id="passlength2" />
                    <span id="validatePasslength2"><?php if ($error) { echo $error['msg']; } ?></span>
                </div>

这是 Javascript:

    var r = $('#passlength').val()
;
        var validatePasslength2 = $('#validatePasslength2');
        $('#passlength2').keyup(function () {

            var t = this;
            if (this.value != this.lastValue) {
                if (this.timer) clearTimeout(this.timer);
                validatePasslength2.removeClass('error').html('<img src="../../images/layout/busy.gif" height="16" width="16" /> checking availability...');

                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'ajax-validation.php',
                        data: 'action=check_passlength2&passlength=' + r + '&passlength2=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            validatePasslength2.html(j.msg);
                        }
                    });
                }, 200);
                this.lastValue = this.value;
            }
        });

这里是php:

//Check for passlength
        if (@$_REQUEST['action'] == 'check_passlength' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
        // means it was requested via Ajax
        echo json_encode(check_passlength($_REQUEST['passlength']));
        exit; // only print out the json version of the response
    }

    function check_passlength($password) {
//        global $taken_usernames, $usersql;
        $resp = array();
//        $password = trim($password);
      if (!preg_match('/^[0-9]{1,30}$/', $password)) {
        $resp = array("ok" => false, "msg" => "0-9 Only");
    } else if (preg_match('/^[0-9]{1,2}$/', $password)) {
                        $resp = array("ok" => false, "msg" => "Password too short");
        } else if (preg_match('/^[0-9]{6,30}$/', $password)) {
                                $resp = array("ok" => false, "msg" => "Password too long");
    } else {
        $resp = array("ok" => true, "msg" => "Password ok");
    } 

        return $resp;
    }


//Check for passlength2
        if (@$_REQUEST['action'] == 'check_passlength2' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
        // means it was requested via Ajax
        echo json_encode(check_passlength2($_REQUEST['passlength'],$_REQUEST['passlength2']));
        exit; // only print out the json version of the response
    }

    function check_passlength2($password,$password2) {
//        global $taken_usernames, $usersql;
        $resp = array();
//        $password = trim($password);
      if (!preg_match('/^[0-9]{1,30}$/', $password2)) {
                $resp = array("ok" => false, "msg" => "0-9 Only");
        } else if (preg_match('/^[0-9]{1,2}$/', $password2)) {
                                $resp = array("ok" => false, "msg" => "Password too short");
        } else if (preg_match('/^[0-9]{6,30}$/', $password2)) {
                                $resp = array("ok" => false, "msg" => "Password too long");
        } else if ($password !== $password2) {
                                $resp = array("ok" => false, "msg" => "Passwords do not match");
        } else {
                $resp = array("ok" => true, "msg" => "Password ok");
        }

        return $resp;
    }

我很确定这是 javascript 的问题,因为如果我设置 var r = 1234;有用。有什么想法吗??

【问题讨论】:

  • 你没有提到什么不起作用。
  • 您可能希望确保您的&lt;label&gt; 标签在其“for”属性中引用正确的字段。

标签: php javascript jquery validation forms


【解决方案1】:

您只想查看密码是否匹配,并且在最小长度和最大长度之间?以上是不是有点矫枉过正?我错过了什么吗?

可以单独使用js查看第一个密码字段的长度,然后在第二个字段的onblur事件中查看field1==field2。

我注意到,第二个字段的标签有错误的“for”属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 2019-10-02
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多