Waiting for Google Geocoder results in a jquery custom validation method

无人久伴 提交于 2019-12-02 13:38:00
Sparky

As I mentioned in my comment, pull the default onkeyup function from the plugin and modify it for your over-ride.

$("#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 );
            }
        }
    },
    ....

In the proof-of-concept DEMO below, hit the submit button to first get past the "lazy" validation, then compare the behavior of the two fields. The first field is validating for an email address on every keyup event. The second field is not validating for email on keyup events at all, only on focusout and submit.

DEMO: http://jsfiddle.net/vbcpyv3q/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!