0

My question is similar to this one: Call javascript function from jquery success; but I'm not using AJAX.

Very simply, I'm validating a form (just the zipcode) as part of a Google Maps web app. The validation code is as such:

$(document).ready(function($)
 {
  $("#clientInput").validate
   ({
    debug: true,

    rules:{zipcode: {required:true, minlength:5, maxlength:10, digits: true}},

    messages: {zipcode: {required: "You need to provide at least a zipcode.", minlength: "Please enter at least 5 digits", maxlength: "You've entered too many digits, please re-enter."}},


   });

   searchlocations();
});

The bold function call is what's stymieing me, it's a call to the Google Map API function but it never gets called. The Google Map javascript is loaded before this code (although it's in a separate file) and before I added the validation code I called the function through the Submit button's onclick() event.

How can I get this function called on successful validation of the form?

TIA!

2 Answers 2

1

You need to make the function the submitHandler for the validation.

Sign up to request clarification or add additional context in comments.

Comments

1

Chuck is right on; here's an example to help, though:

$(document).ready(function($)
 {
  $("#clientInput").validate
   ({
    debug: true,

    rules:{zipcode: {required:true, minlength:5, maxlength:10, digits: true}},

    messages: {zipcode: {required: "You need to provide at least a zipcode.", minlength: "Please enter at least 5 digits", maxlength: "You've entered too many digits, please re-enter."}},

    submitHandler: function() {
            searchlocations();
    }

   });

});

If you only need to call searchLocations, you can make it even simpler by just assigning the searchLocations function directly:

    submitHandler: searchLocations

2 Comments

Unfortunately When I tried Attack's example (cut and paste just to make sure I didn't mistype anything) I get an error stating that searchlocations is not defined. I double checked and the file that contains that function is loaded by the page before the validation code so I'm a bit confused why I'm getting this error.
It sounds like it can't find the searchLocations function, which would mean the searchLocations is probably nested somewhere. How and where is the searchLocations function defined?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.