I've seen tons of samples of how to use jQuery Validation on a select list - and I'm attempting to implement one, but it just isn't functioning for me. I'm really new to the plugin in general and am just completely baffled about what I am doing wrong.
The intended behavior is that it should not validate if the user has left the select menu on "default". They have to make a selection.
I start by wiring up the new validator method.
Javascript
(function($) {
$.validator.addMethod('mustbe', function(value, element, params) {
var testValue = params['propertyvalue'];
var condition = params['condition'];
if ((condition == '0') && (value != testValue)) return true;
if ((condition == '1') && (value == testValue)) return true;
return false;
});
})(jQuery);
$(document).ready(function() {
$("#form1").validate();
$('#form1').submit(function() {
alert($('#form1').valid());
});
});
Then I followup by adding the metadata to the actual HTML.
HTML
<form id="form1" action="">
<select id="select_list" data-val="true"
data-val-mustbe="You must select a value"
data-val-mustbe-condition="0"
data-val-mustbe-propertyvalue="default"
data-val-required="You must select a value" >
<option value="default">Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="submit" id="submit" value="Submit" />
</form>
And it just refuses to validate. Here is a link to a fiddle, too :