Waiting for Google Geocoder results in a jquery custom validation method

前端 未结 1 517
名媛妹妹
名媛妹妹 2021-01-29 14:19

I know Google\'s Geocoder service is asynchronous, but I need a way to return true or false to my custom jQuery Validate method after the google geocoder results have returned a

相关标签:
1条回答
  • 2021-01-29 14:54

    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/

    0 讨论(0)
提交回复
热议问题