Disable jQuery UI Calendar Past And Future Date In ASP.NET MVC

Background

Three years before I wrote the following article: Disable Future and past date in ASP.NET, which has huge response. Currently it has 68.2 k views. So by considering the same requirement in ASP.NET MVC I decided to write same type of article using ASP.NET MVC with the help of jQuery UI library. So let us implement this requirement step by step,
  1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".

  2. "File", then "New" and click "Project", then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click on OK.

  3. Choose MVC empty application option and click on OK
Step 2: Add model class.

Right click on Model folder in the created MVC application and add class named EmpModel and write the following line of code,

EmpModel.cs
  1. public class EmpModel    
  2. {   
  3.    [Display(Name ="Enter Date")]    
  4.    public DateTime EnterDate { getset; }    
  5. }   
Step 3: Home controller

Right click on Controller folder in the created MVC application and add the controller class, 

Now after selecting controller template, click on add button then the following window appears,



Specify the controller name and click on Add button,

HomeController.cs
  1. public class HomeController : Controller    
  2. {    
  3.     // GET: Home    
  4.     public ActionResult Index()    
  5.     {    
  6.         return View();    
  7.     }    
  8. }  
In the above code, The only action result method named Index handle the user request when the user request for the view named Index.cshtml which will be added later .

Step 4: Reference jQuery UI.

Reference jQuery UI css and js library reference as there are many ways to add the reference of jQuery library into our project. The following are some methods,
  • Using NuGet package manager, you can install library and reference into the project.
  • Use CDN library provided by Microsoft, jQuery, Google or any other which requires active internet connection.
  • Download jQuery files from jQuery official website and reference into the project.

In this example we will use jQuery CDN library.

 As we know how to use jQuery UI calendar we need to use datepicker function and to set or allow specific date range in calendar we need to use the following properties of datepicker function,
  • yearRange: It sets the year range for calendar control such as how many past and future years to display in calendar.
  • minDate :  It sets the minimum date for calendar control .
  • maxDate : it sets the maximum date range for calendar control .

Step 5: Create jQuery function to invoke datepicker function,

  1. $(document).ready(function() {  
  2.     $("#EnterDate").datepicker({  
  3.         dateFormat:"dd-mm-yy",  
  4.         minDate: -0,  
  5.         maxDate: "+0M +0D"  
  6.   
  7.     });  
  8. }); 
For making the above function yo work don't forget to add the reference of the following jQuery CDN library as,
  1. <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">    
  2. <script src="//code.jquery.com/jquery-1.10.2.js"></script>    
  3. <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>  
Step 6: Add View.

Add strongly typed view named index from EmpModel class as,


After adding necessary code, files and logic the Index.cshtml code will look like the following,

Index.cshtml
  1. @model DissablePastandFutureDateInMVC.Models.EmpModel  
  2.   
  3. @{  
  4.     ViewBag.Title = "www.compilemode.com";  
  5. }  
  6.   
  7. <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">  
  8. <script src="//code.jquery.com/jquery-1.10.2.js"></script>  
  9. <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>  
  10.   
  11. <script>  
  12.   
  13.   $(document).ready(function() {  
  14.       $("#EnterDate").datepicker({  
  15.           dateFormat:"dd-mm-yy",  
  16.           minDate: -0,  
  17.           maxDate: "+0M +0D"  
  18.   
  19.       });  
  20.   });  
  21. </script>   
  22.   
  23.   
  24. @using (Html.BeginForm())   
  25. {  
  26.     @Html.AntiForgeryToken()  
  27.       
  28.     <div class="form-horizontal">  
  29.          
  30.         <hr />  
  31.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  32.         <div class="form-group">  
  33.             @Html.LabelFor(model => model.EnterDate, htmlAttributes: new { @class = "control-label col-md-2" })  
  34.             <div class="col-md-10">  
  35.                 @Html.EditorFor(model => model.EnterDate, new { htmlAttributes = new { @class = "form-control" } })  
  36.                 @Html.ValidationMessageFor(model => model.EnterDate, ""new { @class = "text-danger" })  
  37.             </div>  
  38.         </div>  
  39.   
  40.         
  41.     </div>  

Now run the application and put mouse pointer into the textbox the calendar control will popup as in the following screenshot,
 
 
In the above screenshot you can see that only current date is enabled and all other dates are disabled. Now let us change the maxDate property to enable only five days from current date,
  1. $(document).ready(function() {  
  2.     $("#EnterDate").datepicker({  
  3.         dateFormat:"dd-mm-yy",  
  4.         minDate: -0,  
  5.         maxDate: "+0M +4D"  
  6.   
  7.     });  
  8. }); 
Now run the application the output will be as follows:
 
 
In the preceding screenshot you can see that only five dates are available including current date. From all the preceding examples, I hope you learned how to disable past and future dates using jQuery UI calendar control in ASP.NET MVC.

Note:
  • For the detailed code, please download the sample zip file.
  • You need to use the jQuery library and if you are using CDN library then it requires active internet connection .
Summary

I hope this article is useful for all readers. If you have a suggestion then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all