0
我有一个表单,我正在验证JS和PHP。从我尝试检查密码是否匹配时开始,一切都进行得如此顺利。Jquery检查密码与php + Json匹配
下面的形式:
<div>
<label for="passlength">Password, valid: 0-9</label>
<input type="text" name="passlength" value="<[email protected]$_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="<[email protected]$_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;有用。有任何想法吗??
你没有提到什么不工作。 – 2010-03-09 10:48:55
您可能想要确保您的'