views:

75

answers:

1

I am new with jQuery. I have a servlet based application which render an HTML form. This form is submitted via a function and the submit button IS NOT submit button in HTML. It's a regular button that calls a javascript function to do the submission.

When running the submission function, the servlet also run a function called doCheck() which is in javascript. This doCheck() function can be defined per page basis. if doCheck returns false, then the servlet will stop the submission; else it will submit the form.

I want to use jQuery validation to fit the servlet application. Is there a way to customize jQuery validation to be triggered manually (ie. only when doCheck() is executed) but at the same time provides continuous check on the UI (ie. if one type invalid number in date field, it will gives the appropriate msg when onblur event happen)? Is there a way to make jQuery validation return true or false? Thank you.

+1  A: 

You have a few options to accomplish exactly what you want. If you replace your doCheck() with this:

function doCheck() {
  $("#myForm").validate();
  return $("#myForm").valid();
}

You are now calling the validation library to check if the form is valid and return that. This will trigger the display of messages for invalid fields, etc.

See here for a full list of validation methods.

However, by default, the validation plugin hooks up to the submit method on the form and stops there, so no matter how you're calling .submit() it should work already, stopping the submission if anything is invalid. You can prevent this with the onsubmit: false option if you want.

Nick Craver