I think you can implement this using custom validation in MVC. Try using this:
[ValidateDateRange] public DateTime StartWork { get; set; }
Here is your custom verification implementation:
namespace MVCApplication { public class ValidateDateRange: ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
UPDATE:
You can also pass date ranges as parameters to make validation common:
[ValidateDateRange(FirstDate = Convert.ToDateTime("01/10/2008"), SecondDate = Convert.ToDateTime("01/12/2008"))] public DateTime StartWork { get; set; }
User check:
namespace MVCApplication { public class ValidateDateRange: ValidationAttribute { public DateTime FirstDate { get; set; } public DateTime SecondDate { get; set; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
UPDATE 2: (for the client side) A very simple jQuery logic should do the client validation. Check below:
$(document).ready(function(){ $("#btnSubmit").click(function(){ var dt = $("#StartWork").val(); var d = new Date(dt); var firstDate = new Date("2008-01-10"); var secondDate = new Date("2008-01-12"); if(d>= firstDate && d<= secondDate) { alert("Success"); } else { alert("Date is not in given range."); } }); });
Please check this JSFiddle to see a working demo: Checking the date range
Saket Nov 28 '14 at 8:16 2014-11-28 08:16
source share