【发布时间】:2013-12-07 12:31:06
【问题描述】:
我正在使用带有jQuery Validation Plugin的自定义验证码
我的配置如下:
$('#contactform').validate({
onkeyup: function(element, event) {
$(element).valid();
},
errorElement: 'span',
rules: {
contactform_captcha: {
minlength: 6,
maxlength: 6,
required: true,
remote: /path/to/captcha-check.php
},
},
messages: {
contactform_captcha: {
required: "No code, no email",
minlength: function () {
return [
'Enter at least ' + (6 - parseInt($('#contactform_captcha').val().length)) +
' more character(s) to go.'];
},
remote: function () {
$("#captchaimage").attr("src", /path/to/new/captcha.php+"?r=" + Math.random());
return ['Wrong code, therefore try again'];
}
},
},
success: function(label) {
label.addClass("valid").text("Well done!")
},
submitHandler: function(form) {
//submit form
}
});
$('span[class^="error"]:not(.valid)').remove();
此代码的作用是验证是否有 6 个字符,然后将这 6 个字符发送到 captcha-check.php。如果一切正常,captcha-check.php 将返回 true,否则将返回 false,然后调用此函数:
remote: function () {
$("#captchaimage").attr("src", /path/to/new/captcha.php+"?r=" + Math.random());
return ['Wrong code, therefore try again'];
}
在false 上,消息功能将更新图像#captchaimage 的新src。一切正常,但只是第一次。我猜远程响应消息被缓存了。
另外,如果我在远程远程响应消息中输出Math.random(),它不会更新,可能有问题。
【问题讨论】:
-
如前所述,您的
remote方法在每次击键时都会被调用,因此,您尝试在每次击键时更改验证码图像。在这种情况下,用户不可能输入正确的验证码。
标签: jquery jquery-validate captcha