等待 Google Geocoder 会产生 jquery 自定义验证方法
Waiting for Google Geocoder results in a jquery custom validation method
我知道谷歌的地理编码器服务是异步的,但我需要一种方法在谷歌地理编码器结果返回后而不是之前向我的自定义jQuery Valid方法返回true或false。(例如,服务将查找邮政编码,如果找到返回 true,否则返回 false)。
编辑 - jQuery Validate remote
方法是否有效?
目前我对元素有一组规则,但是当我在下面测试此代码时,getLocation
方法会在加载代码后立即调用,而不是像我想要的那样输入第 5 位数字时调用。
$('#Location').rules("add", {
required: true,
minlength: 5,
maxlength: 5,
messages: {
required: "Required input",
minlength: jQuery.validator.format("Please, {0} characters are necessary"),
maxlength: jQuery.validator.format("Please, {0} characters are necessary")
},
remote: getLocation()
});
function getLocation() {
var i = 0;
}
这是我的自定义方法。
$.validator.addMethod("validateZipCode", function(value, element) {
var isValidZipCode = GetGoogleGeocoderResultsByZip(value);
return zipCodeIsValid;
}, "Invalid location");
//the geocode results work something like this, but I need to wait to return true/false
function GetGoogleGeocoderResultsByZip(zipCode) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode("componentRestrictions": {
"country": "United States",
"postalCode": zipCode
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results.length >= 1) {
return true;
}
return false;
}
}
};
正如我在评论中提到的,从插件中提取默认的onkeyup
函数并对其进行修改以进行覆盖。
$("#contact").validate({
onkeyup: function(element, event) {
if (element.name == "zip-code") {
return; // no onkeyup validation at all
} else {
// default onkeyup validation in here
var excludedKeys = [
16, 17, 18, 20, 35, 36, 37,
38, 39, 40, 45, 144, 225
];
if ( event.which === 9 && this.elementValue( element ) === "" || $.inArray( event.keyCode, excludedKeys ) !== -1 ) {
return;
} else if ( element.name in this.submitted || element.name in this.invalid ) {
this.element( element );
}
}
},
....
在下面的概念验证 DEMO 中,点击submit
按钮首先通过"懒惰"验证,然后比较两个字段的行为。 第一个字段是验证每个keyup
事件的电子邮件地址。 第二个字段根本不验证keyup
事件的电子邮件,仅在focusout
和submit
上验证。
演示:http://jsfiddle.net/vbcpyv3q/
相关文章:
- jQuery自定义验证比较多个输入的序列
- jquery自定义事件混淆
- 无法使用jQuery自定义内容滚动条将自定义滚动条添加到iframe
- HTML/JQuery 自定义表单
- 试图使一个事件依赖于另一个事件,并使用jquery自定义事件传递数据
- jQuery自定义验证只显示最后一个字段的错误
- 带有prev和next的jquery自定义循环插件
- 如何解决jquery自定义定时器问题
- jQuery自定义滚动条插件没有't使用水平滚动进行更新:true
- jQuery自定义模板工作不正常
- jQuery 自定义输入触发器复选框
- 使用 Jquery 自定义按钮和滑块(大小、位置、标题大小)
- 等待 Google Geocoder 会产生 jquery 自定义验证方法
- 触发不同实例设置的 jQuery 自定义事件
- jQuery 自定义内容滚动器在引导表响应式上不起作用
- JQuery 自定义警报框自动运行
- jQuery 自定义事件在全局范围内工作,但在对象之间不起作用
- Joomla 中的 jquery 自定义插件不起作用
- 无法识别 JQuery 自定义插件函数
- JQuery 自定义灯箱 - 不透明度淡入淡出