Last active
December 15, 2015 04:58
-
-
Save JustinMason/5205136 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
// The adapter to support ASP.NET MVC unobtrusive validation | |
$.validator.unobtrusive.adapters.add('rangedate', ['minselector', 'maxselector', 'mindate', 'maxdate', 'nullable'], | |
function(options) { | |
var params = { | |
minSelector: options.params.minselector, | |
maxSelector: options.params.maxselector, | |
minDate: options.params.mindate, | |
maxDate: options.params.maxdate, | |
nullable: options.params.nullable == "true" | |
}; | |
options.rules['rangeDate'] = params; | |
if (options.message) { | |
options.messages['rangeDate'] = options.message; | |
} | |
}); | |
// The validator function | |
$.validator.addMethod('rangeDate', | |
function(value, element, param) { | |
var dateVal = parseDate(value, "G"); // .Net "G" DateTime format, set as the same format used by the ModelClientValidationRule. | |
var minDate; | |
var maxDate; | |
if (value.length < 1 && param.nullable) | |
return true; | |
if (param.minDate.length > 0) { | |
minDate = parseDate(param.minDate, "G"); | |
} else { | |
var minControl = $('#' + param.minSelector); | |
if (minControl.exists()) { | |
minDate = parseDate(minControl.val(), "G"); | |
} else { | |
return false; | |
} | |
} | |
if (param.maxDate.length > 0) { | |
maxDate = parseDate(param.maxDate, "G"); | |
} else { | |
var maxControl = $('#' + param.maxSelector); | |
if (maxControl.exists()) { | |
maxDate = parseDate(maxControl.val(), "G"); | |
} else { | |
return false; | |
} | |
} | |
return minDate <= dateVal && dateVal <= maxDate; | |
} | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment