/*https://thewayofcode.wordpress.com/2012/01/18/custom-unobtrusive-jquery-validation-with-data-annotations-in-mvc-3/*/ /*METHODS*/ // Value is the element to be validated, params is the array of name/value pairs of the parameters extracted from the HTML, element is the HTML element that the validator is attached to $.validator.addMethod("datenotexceedprop", function (value, element, params) { //console.log("val: " + value + "/" + Date.parse(value) + "; params: " + $(params).val() + "/" + Date.parse($(params).val())); return value == "" || $(params).val() == "" || (Date.parse(value) <= Date.parse($(params).val())); }); $.validator.addMethod("dategreaterthanorequal", function (value, element, params) { //console.log("val: " + value + "/" + Date.parse(value) + "; params: " + $(params).val() + "/" + Date.parse($(params).val())); return $(params).val() == null || Date.parse($(params).val()) == NaN || (Date.parse(value) >= Date.parse($(params).val())); }); $.validator.addMethod("eitherofrequired", function (value, element, params) { //console.log("val: " + value + "; params: " + $(params).val()); if ($(params).length > 0) return !(!value && !$(params).val()); else return !!value; }); /*ADAPTERS*/ /* The adapter signature: adapterName is the name of the adapter, and matches the name of the rule in the HTML element. params is an array of parameter names that you're expecting in the HTML attributes, and is optional. If it is not provided, then it is presumed that the validator has no parameters. fn is a function which is called to adapt the HTML attribute values into jQuery Validate rules and messages. The function will receive a single parameter which is an options object with the following values in it: element The HTML element that the validator is attached to form The HTML form element message The message string extract from the HTML attribute params The array of name/value pairs of the parameters extracted from the HTML attributes rules The jQuery rules array for this HTML element. The adapter is expected to add item(s) to this rules array for the specific jQuery Validate validators that it wants to attach. The name is the name of the jQuery Validate rule, and the value is the parameter values for the jQuery Validate rule. messages The jQuery messages array for this HTML element. The adapter is expected to add item(s) to this messages array for the specific jQuery Validate validators that it wants to attach, if it wants a custom error message for this rule. The name is the name of the jQuery Validate rule, and the value is the custom message to be displayed when the rule is violated. */ $.validator.unobtrusive.adapters.add("datenotexceedprop", ["otherpropertyname"], function (options) { options.rules["datenotexceedprop"] = "#" + options.params.otherpropertyname; options.messages["datenotexceedprop"] = options.message; }); $.validator.unobtrusive.adapters.add("dategreaterthanorequal", ["otherpropertyname"], function (options) { options.rules["dategreaterthanorequal"] = "#" + options.params.otherpropertyname; options.messages["dategreaterthanorequal"] = options.message; }); $.validator.unobtrusive.adapters.add("eitherofrequired", ["otherpropertyname", "singleerrormessage"], function (options) { options.rules["eitherofrequired"] = "#" + options.params.otherpropertyname; if ($(options.rules["eitherofrequired"]).length > 0) options.messages["eitherofrequired"] = options.message; else options.messages["eitherofrequired"] = options.params.singleerrormessage; });