0

I have a form with two submit buttons on it. Buttons must be of submit type and I can't change it.

How can I make jquery not to validate but submit the form when first button is pressed and validate and submit when second button is pressed?

thank you

2
  • What is the purpose of the buttons? i think there is a different purpose two each button? Commented Feb 13, 2011 at 15:57
  • one button allows user to find their address by postcode (submits form with postcode filled, I don't need to validate other fields in this case), second button actually submits the form (needs full validation) Commented Feb 13, 2011 at 15:59

3 Answers 3

3

Ok, I found a solution which is ridiculously easy as usual, the only thing I need to do is to set class="cancel" to the first button and it'll skip validation on submit.

Found here http://forum.jquery.com/topic/stop-a-specific-button-calling-validation

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

2 Comments

Oh the wonders of black magic :D. However, if the user first clicks on the validating button, the validation error messages will be left on screen when she clicks on the .cancel button. Might not be an issue, and can be fixed with a click event on the .cancel button.
Yeah.. well.. it's kinda weird but ok :D
3

event.originalEvent.explicitOriginalTarget is a Gecko-specific property (see this question and answer).

The following should work:

var doValidate;
$('#validating-button').click(function () { doValidate = true; });
$('#non-validating-button').click(function () { doValidate = false; });
$('#form').validate({
    rules: {
        myinputname: {
            required: function () { return doValidate; }
        }
    }
});

Comments

0

EDIT: check questioneer's answer :)

First btn clicked, do validation stuff and check validation (just try it/improve it)... Second Btn clicked, just do nothing (submit form)

$('#formid').submit(function(event) {
    if (event.originalEvent.explicitOriginalTarget.id == "firstButtonID") {
       $(this).validate();
       if(!$(this).valid()){
            event.preventDefault();
            return false;
       }
    }
});

4 Comments

I'm not very good in js, but I think this way will not work, first of all I need to submit the form in both cases, in the second place Jquery validates the form before submit is fired
Ok.. check my code again, it will submit in both cases, but for the first button it will validate befor submission :)
explicitOriginalTarget is Gecko-only: stackoverflow.com/questions/179826/…
YOu're right.. it was just a starting tip to help him find a solution :) .. would be helpful trying to find a way to obtain the clicked btn id in a cross-browser way..

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.