1

I am using jquery validation plugin for US ZIPCODE CHECK ,its working fine, but I have to push the submit button TWICE to get the form submitted,

Note : if I press enter that it submits the form properly, but it doesnt work with click event

I already have checked this stackoverflow answers but they didnt helped me out

Why do you have to submit twice before jQuery Validate submitHandler is triggered?

jquery validate need to click submit twice to submit form

jQuery using .on and .validate must submit form twice to validate

Below is my Javascript code

var ans;
$(document).ready(function(e)
        {
           jQuery.validator.addMethod("zipcode", function(e)
            {
               $.ajax(
                        {
                            url: "http://zip.elevenbasetwo.com",
                            cache: false,
                            async: false,
                            dataType: "json",
                            type: "GET",
                            data: "zip=" + $("#zip").val(),
                            success: function()
                            {
                                ans = true;
                            },
                            complete: function()
                            {
                                ans = true;
                            },
                            error: function()
                            {
                                ans = false;
                            }
                        }
                );
                return ans;
            });
            $("#register-form2").validate({
                debug: true,
                onkeyup: false,
                rules: {
                    zip: {
                        required: true,
                        zipcode: true,
                        minlength: 5
                    },
                    email: {
                        required: true,
                        email: true
                    }
                },
                messages: {
                    zip: {
                        required: "Please enter a USA zip code",
                        zipcode: "Please enter a valid USA zip code"
                    },
                    //  zip: "Please enter a valid USA zip code.",
                    email: "Please enter a valid email address."
                },
                submitHandler: function(form)
                {
                    alert('hi');
                    form.submit();
                }
            });
        });

Please mark that if I dont use ajax function in my custom method than it just works fine, but i am not able to solve it.

below is my HTML code

<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" class="wpcf7-form" id="register-form2" novalidate>
<input name="email" required id="email" type="text" placeholder="Email*">
<input type="text" name="zip" id="zip" required size="40" maxlength="15" placeholder="Zip code* (USA only)">
<input name="Form_submit" class="btn btn-primary wpcf7-form-control  wpcf7-submit" id="Submit" value="Sign-up" type="submit">
</form>
Community
  • 1
  • 1
Nishant Solanki
  • 2,119
  • 3
  • 19
  • 32

0 Answers0