Instantly share code, notes, and snippets.

Embed
What would you like to do?
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