I'm having some trouble finding a nice solution for validating two different forms on two tabs. I want both to be valid to post data. I'm using the bootstrap framework for the tabs, and jquery validation.
Here's the example: http://jsfiddle.net/SLsde/
HTML
<div class="container">
<ul class="nav nav-pills" id="tabs">
<li class="active"><a href="#tab1" data-toggle="pill">Tab 1</a></li>
<li><a href="#tab2" data-toggle="pill">Tab 2</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<form class="form" id="form1">
<div class="form-group">
<label>Input 1</label>
<input id="input1" name="input1" type="text" class="form-control" />
</div>
<input id="Submit1" type="submit" class="btn btn-primary" value="Submit" />
</form>
</div>
<div class="tab-pane" id="tab2">
<form class="form" id="form2">
<div class="form-group">
<label>Input 2</label>
<input id="input2" name="input2" type="text" class="form-control" />
</div>
<input id="Submit2" type="submit" class="btn btn-primary" value="Submit" />
</form>
</div>
</div>
</div>
JS
var form1 = $("#form1").validate({
ignore: '',
rules: {
"input1": "required"
}
});
var form2 = $("#form2").validate({
ignore: '',
rules: {
"input2": "required"
}
});
$("#form1").submit(function (e) {
e.preventDefault();
if (form1.valid() && form2.valid()) {
alert("Post Data");
} else {
alert("Not Valid");
}
});
$("#form2").submit(function (e) {
e.preventDefault();
if (form1.valid() && form2.valid()) {
alert("Post Data");
} else {
alert("Not Valid");
}
});
If you try and submit on one tab and with no data on the other it'll still say it's valid. If you submit with it empty then go to other tab to submit it will then say false. I tried using the ignore: '' thing that many suggest, but I couldn't find many examples of a form on each tab.
Any ideas would be helpful.