views:

420

answers:

4

hello, this is a simple question. I have a form that is being validated using jquery's .validate function.

The problem is that validating only seems to work when I submit the form using: <input type='submit' />

However, I would like to use my own custom button using instead: <button type="submit">click to submit</button> .... or use something like... <a onclick="submitFunction ()">click to submit</a>

These last two options however, do not trigger jquery's validate function. Is there a way around this? Thanks for the help in advance!

A: 

What you need to do in your custom submit function is as follows

jQuery('#formId').submit();

You can create your own validation and when the form is valid use the above method to submit it. This is the same as clicking a submit button in the form. #formId is the id you've given your form. There are several other ways you can reference the form as well.

Mark
A: 

If all you want to do is change what the submit button says, you could use

<input type="submit" value="click to submit" />

Update To work with the validation plugin, this is really what I would suggest. If you want to do anything else when they click that button, you can use submitHandler as an option: http://docs.jquery.com/Plugins/Validation/validate#toptions .

D_N
+2  A: 

The jQuery Validation plugin is tied to the submit event in jQuery. If you want to trigger this from anywhere else (i.e. any other event handler), you will have to call submit manually, which will trigger the validation to occur.

Note that after calling validate to set up validation on your form you can store the Validator instance it returns and call the form method on it if you want to perform the validation separately from the submission of the form.

casperOne
thanks casper.. question for you, what do you mean by "If you want to trigger this from anywhere else (i.e. any other event handler), you will have to call submit manually"?I've tried manually submitting via javascript, as well as a button type of "submit", but it doesn't seem to trigger the validation method. Is there something else I'm missing?
Rees
jQuery('#formId').submit();Thats how you can submit it manually. I posted the answer below.
Mark
I've tried manually submitting via jquery $('#myform').submit(); --and now it validates, but it will immediately submit Even If there are form errors -- which makes me think the submitHandler option isn't being recognized using $('#myform').submit(); --since submitHandler would prevent a form with errors from being submitted. Is there something else I'm missing?
Rees
actually sorry, $('#myform').submit(); is in fact working!! and it doesn't skip over submitHandler.. apparently if you are validating with jquery, you also have to using jquery to submit instead of javascript's document.forms["form"].submit(); Thanks casperone and Mark!
Rees
+1  A: 

As far as I know jQuery doesn't have a "validate" method, but either way, in order to use elements to submit the form just bind the submit event:

$('#formid').submit(function(){
    // validate form
    ...
    // if validation fails, return false;
});
Alex
He's using this: http://plugins.jquery.com/project/validate
D_N
I figured, thanks :)
Alex