I have a group of buttons, and ONE of them needs to be clicked in order to proceed to the next form:
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" data-bind="click: sportYes">Yes</button>
<button type="button" class="btn" data-bind="click: sportBasic">Basic</button>
<button type="button" class="btn" data-bind="click: sportNo">No</button>
</div>
I am using jquery validate, and I want to write a validate
method but I can't use my typical approach, such as giving a text field a value of required, e.g.
$("#FormThatNeedsToBeValidated").validate({
rules: {
textinput1: {
required: true
},
textinput2: {
required: true
}}});
Even if I knew the syntax, I don't think this would help, since not ALL buttons are required.
Is there a way to leverage the active
attribute that's rendered when one of my three buttons is clicked?
A <button>
is not eligible for validation using this plugin.
This plugin will only work on the following eight kinds of data input elements (each must have a unique name
attribute), which also must be contained within a form element:
<form>
<!-- Textbox Input - Also including the various HTML5 input types -->
<input type="text" name="something" />
<!-- Password Input -->
<input type="password" name="pw" />
<!-- Radio Button -->
<input type="radio" name="foo" />
<!-- Checkbox -->
<input type="checkbox" name="bar" />
<!-- File Upload -->
<input type="file" name="upload" />
<!-- Hidden Input - 'ignore: []' must be defined in the options -->
<input type="hidden" name="hide" />
<!-- Select Dropdown -->
<select name="foobar"> ... </select>
<!-- Text Area Box -->
<textarea name="barfoo"></textarea>
</form>
Also see the "reference" page in the docs: http://jqueryvalidation.org/reference/
To achieve the desired effect, you can test & submit the form upon clicking a particular button. Use the .valid()
method to test/check and submit()
to submit the form.
HTML:
<button type="button" class="btn" id="sportYes">Yes</button>
<button type="button" class="btn" id="sportBasic">Basic</button>
<button type="button" class="btn" id="sportNo">No</button>
jQuery:
$(document).ready(function() {
$("#FormThatNeedsToBeValidated").validate({ // <- initialize plugin on form
// rules & options
});
$('#sportYes').on('click', function() {
// stuff to do when this button is clicked
if ($("#FormThatNeedsToBeValidated").valid()) { // <- test validity
// stuff to do on valid form
$("#FormThatNeedsToBeValidated").submit(); // <- submit the valid form
}
});
$('#sportBasic').on('click', function() {
// stuff to do when this button is clicked
});
$('#sportNo').on('click', function() {
// stuff to do when this button is clicked
});
});