Hi and thanks for taking the time to answer my question.
I need to create a validation rule within the validation engine thay will not allow values of 0. (meaning at least one element has to be selected). I have a select element and the default (Please select an item) has a value of 0 because it is not required. But once a checkbox is selected I need to make it required. I cannot add the class 'validate[required]' because 0 is a valid value. So my question would be how to make a custom rule which will not allow values of 0 to be passed to the controller.
Thanks again
I think it's no need in custom validation. You can add required attribute with the help of jquery when checkbox is checked and remove this attribute when it's unchecked(and also check it on document ready for first load). For example:
$('#myCheckBox').click(function () {
if ($(this).is(':checked')) {
$('#myDropDown').removeAttr('required');
}
else {
$('#myDropDown').attr('required', true);
}
});
But in this case your <option>
value should be empty. <option value>Please select an item</option>
(as i know it's usual behaviour of dropdownlists) . And after that jquery validation plugin will automatically validate required attribute.
EDIT
Alright, if you definitely should use custom validation, you can do it this way:
1) Add you validation method( on document ready for instance)
$.validator.addMethod("checkIfNotZero", function (value, element) {
return this.optional(element) || ($('#checkbox').is(':checked')&&value!=0);
}, "Please, select value");
$.validator.addClassRules("mySpecificClass", { checkIfNotZero: true });
2) Mark you dropdownlist with class="mySpecificClass"
3) And check if $('#myDropDown').valid()
where it's necessary (or for instance call $(form).validate()
in onSubmit event handler).
I think that's all. Hope it will help.