[英]JQuery Validate Rule not working
Fixed - see below :) 固定-见下文:)
I'm using the jQuery validate plugin and for some reason I can't get it to return a valid field - always returns invalid. 我正在使用jQuery validate插件,由于某种原因,我无法使它返回有效字段-始终返回无效字段。
http://plugins.jquery.com/project/jqueryvalidate http://plugins.jquery.com/project/jqueryvalidate
Here's what I have 这就是我所拥有的
$(function(){
$("#address").validate({
valid: function(val){
$.getJSON("http://maps.google.com/maps/geo?q="+ escape(val) +"&key=ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA&sensor=false&output=json&callback=?",
function(data, textStatus)
{
if(data.Status.code=='200')
{
return true;
}else{
return false;
}
});
},
errorMessage: function(val){
return "must geo code";
},
appendCompletionIcon: true
});
});
I can see the GEO code requests coming back as found or not found for any particular address but the validation still fails - if I amend the function as follows I do get an alert yes or alert no in relation to a valid or invalid address but the validation still fails. 我可以看到针对任何特定地址找到的或未找到的GEO代码请求都返回了,但验证仍然失败-如果按以下方式修改功能,则我会收到关于是有效地址还是无效地址的警报,是或否,但是验证仍然失败。
function(data, textStatus)
{
if(data.Status.code=='200')
{
alert('yes');
return true;
}else{
alert('no');
return false;
}
});
Any help would be appreciated 任何帮助,将不胜感激
I found a way to hack around this, it's not extremely pretty as it takes two geo code calls to complete. 我找到了解决此问题的方法,因为它需要两次地理代码调用才能完成,所以它不是很漂亮。 Good enough for my purposes though.
虽然对我来说足够好。
var geocode = false;
function geoadd()
{
$.getJSON("http://maps.google.com/maps/geo?q="+ $("#address").val() +"&key=ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA&sensor=false&output=json&callback=?",
function(data, textStatus)
{
if(data.Status.code=='200')
{
geocode = true;
}else{
geocode = false;
}
});
}
$(function(){
$("#address").validate({
valid: function(val){
geoadd();
return geocode;
},
errorMessage: function(val){
return "must not be blank";
},
appendCompletionIcon: true
});
});
Your validation fails regardless of ajax call status because the value returned from your valid
handler is null, not true or false. 不管ajax调用状态如何,验证都会失败,因为从
valid
处理程序返回的值为null,而不是true或false。
The true or false values are returned by the complete handler of the ajax which is not the same as returning true/false value from your valid
function. true或false值由ajax的完整处理程序返回,这与从
valid
函数中返回true / false值不同。
To solve this issue you would have to either make a synchronous ajax call (I wouldn't recommend it since it locks-up user interface while waiting for the response) or change your validation plugin for some other that supports validation using ajax calls. 要解决此问题,您要么必须进行同步ajax调用(我不建议这样做,因为它在等待响应时会锁定用户界面),或者将您的验证插件更改为其他一些支持使用ajax调用进行验证的插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.