Skip to content

Instantly share code, notes, and snippets.

@timhobbs
Created October 22, 2011 19:50
Show Gist options
  • Save timhobbs/1306413 to your computer and use it in GitHub Desktop.
Save timhobbs/1306413 to your computer and use it in GitHub Desktop.
MaximumWeightAttribute custom attribute jQuery unobtrusive client side validation implementation
<script type="text/javascript">
$.validator.addMethod("maximumweight",
function (value, element, parameters) {
var carrier = $("#" + parameters["dependentproperty"]).val();
var carriervalue = parameters["dependentvalue"].toString();
var weightvalue = Number(parameters["weightvalue"]);
if (carrier == carriervalue && value > weightvalue) {
return false;
}
return true;
}
);
$.validator.unobtrusive.adapters.add(
"maximumweight",
["weightvalue", "dependentproperty", "dependentvalue"],
function (options) {
options.rules["maximumweight"] = {
weightvalue: options.params["weightvalue"],
dependentproperty: options.params["dependentproperty"],
dependentvalue: options.params["dependentvalue"]
};
options.messages["maximumweight"] = options.message;
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment