Monday, August 27, 2012

Customized validation for text box based on drop down value

---------- Html -------------

Sale Tax Amount Validation Example

-----End------------- --------------Jquery----------- $.validator.addMethod("taxAmount_validate", function(value, element) { var ddlTaxType= $('#ddlTaxType').val(); if (ddlTaxType== 0 || ddlTaxType== 2) { if (value == 0) { return true; } else { return false; } } if (ddlTaxType== 1) { if (value > 0) { return true; } else { return false; } } }); $("#taxForm").validate({ rules: { txtSaleTaxAmont: { required:function(element) {return $('#ddlPaymentMethod :selected').val()=='2' }, taxAmount_validate: { depends: function(element) {return $('#ddlPaymentMethod :selected').val()=='2' } } } }, messages: { txtSaleTaxAmont: { taxAmount_validate: function() { switch ($('#ddlTaxType').val()) { case '0': case '2': return "Sales Tax must be 0!"; break; case '1': return "Sales Tax must be more than 0!" break; } } } } }); -------------Jquery End---------------

No comments:

Post a Comment