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

Burjua
  • 12,506
  • 27
  • 80
  • 111
  • What is the purpose of the buttons? i think there is a different purpose two each button? – Silagy Feb 13 '11 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) – Burjua Feb 13 '11 at 15:59

3 Answers3

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; }
        }
    }
});
Community
  • 1
  • 1
Bergius
  • 949
  • 6
  • 14
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

Burjua
  • 12,506
  • 27
  • 80
  • 111
  • 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. – Bergius Feb 13 '11 at 16:25
  • Yeah.. well.. it's kinda weird but ok :D – stecb Feb 13 '11 at 16:26
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;
       }
    }
});
stecb
  • 14,478
  • 2
  • 50
  • 68
  • 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 – Burjua Feb 13 '11 at 16:09
  • Ok.. check my code again, it will submit in both cases, but for the first button it will validate befor submission :) – stecb Feb 13 '11 at 16:13
  • explicitOriginalTarget is Gecko-only: http://stackoverflow.com/questions/179826/crossbrowser-equivalent-of-explicitoriginaltarget-event-parameter – Bergius Feb 13 '11 at 16:29
  • 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.. – stecb Feb 13 '11 at 16:31