I am trying to do conditional validation using Jquery.Validate()
I have a select named selectAmount and an inpute named txtAmount
If selectAmount value = "Other"
I'd like for txtAmount to be required.
<div id="container">
<form id="AddForm" method="POST" action="#">
<select name="selectAmount">
<option value="10">$10</option>
<option value="25">$25</option>
<option value="50">$50</option>
<option value="100">$100</option>
<option value="250">$250</option>
<option value="500">$500</option>
<option value="Other">Other</option>
</select>
<input name="txtAmount" type="text" placeholder="Enter Amount" />
<button id="AddToCart">Add to Cart</button>
</form>
</div>
Here's where I'm setting the rules. The problem I'm having is here:
"txtAmount": {
required: function(element){
alert($("#selectAmount").val());
return false;
$('#selectAmount').val() //is always undefined
$(document).ready(function () {
$("#AddForm").validate({
rules: {
"selectAmount" : {
required: true
},
"txtAmount": {
required: function(element){
alert($("#selectAmount").val());
return $("#selectAmount").val() == 'Other';
}
}
},
messages: {
"selectAmount": {
required: "Amount selection is required."
},
"txtAmount": {
required: "Amount is required."
}
},
submitHandler: function (form) { // for demo
return false; // for demo
}
});
});
Can anyone help?