Range Restriction

19 Dec 20224 minutes to read

Range selection in a DateRangePicker can be made-to-order with desire restrictions based on the application needs.

Restrict the range within a range

You can restrict the minimum and maximum date that can be allowed as start and end date in a range selection with the help of min, max properties.

  • min – sets the minimum date that can be selected as startDate.
  • max – sets the maximum date that can be selected as endDate.
@Html.EJS().DateRangePicker("element").Min(ViewBag.minDate).Max(ViewBag.maxDate).Placeholder("Choose a DateRange").Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers
{
    public class DateRangePickerController : Controller
    {
        public ActionResult DateRange()
        {
            ViewBag.minDate= new DateTime(DateTime.Now.Year,DateTime.Now.Month,15);
            ViewBag.maxDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month+1, 15);
            return View();
        }
    }
}

NOTE

 If the value of min or max property is changed through code behind, update the start date and end date properties to set within the range. Or else , if the start and end date is out of specified date range, a validation error class will be appended to the input element. If strictMode is enabled, and both the start, end date is lesser than the min date then start and end date will be updated with min date. If both the start and end date is higher than the max date then start and end date will be updated with max date. Or else, if startDate is less than min date, startDate will be updated with min date or if endDate is greater than max date, endDate will be updated with the max date.

Range span

Span between ranges can be limited to avoid excess or less days selection towards the required days in a range. In this, minimum and maximum span allowed within the date range can be customized by minDays and maxDays properties.

  • minDays - Sets the minimum number of days between start and end date.
  • maxDays - Sets the maximum number of days between start and end date.
@Html.EJS().DateRangePicker("daterangepicker").Placeholder("Select a Range").MinDays(4).MaxDays(10).Render()

Strict mode

DateRangePicker provides an option to limit the user towards entering the valid date. With strict mode, you can set only the valid date. If any invalid range is specified, the date range value resets to previous value. This restriction can be availed by enabling strictMode property as true.

@Html.EJS().DateRangePicker("element").StrictMode(true).Placeholder("Select a Range").Min(ViewBag.minDate).Max(ViewBag.maxDate).StartDate(ViewBag.startDate).EndDate(ViewBag.endDate).Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers
{
    public class DateRangePickerController : Controller
    {
        public ActionResult DateRange()
        {
            ViewBag.minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15);
            ViewBag.maxDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 15);
            ViewBag.startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 20);
            ViewBag.endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 25);
            return View();
        }
    }
}