Range Restriction in EJ2 TypeScript DateRangePicker
4 Sep 20268 minutes to read
Range selection in the DateRangePicker can be customized with the desired restrictions based on the application needs.
Restrict the range within a range
The minimum and maximum date that can be allowed as start and end date in a range selection can be restricted with the help of the 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.
In the following sample, a range can be selected from the 15th day of this month to the 15th day of next month.
import { DateRangePicker } from '@syncfusion/ej2-calendars';
let today: Date = new Date();
let currentYear: number = today.getFullYear();
let currentMonth: number = today.getMonth();
let currentDay: number = today.getDate();
// creates a daterangepicker with min max property
let daterangeObject: DateRangePicker = new DateRangePicker({
//sets the min.
min: new Date(currentYear, currentMonth, 15),
//sets the max.
max: new Date(currentYear, currentMonth+1, 15),
placeholder:"Select a Range"
});
daterangeObject.appendTo('#element');<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 DateRangePicker control</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-lists/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-calendars/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<input id='element' type="text" />
</div>
</body>
</html>If the value of the
minormaxproperties is changed through code, then thestartDate,endDateproperties have to be updated to be set within the range. Or else, if thestartandenddate is out of the specified date range, a validation error class will be appended to the input element. IfstrictModeis enabled, and both the start, end date is lesser than the min date then start and end date will be updated withmindate. If both the start and end date is higher than the max date then start and end date will be updated withmaxdate. Or else, if startDate is less thanmindate, startDate will be updated withmindate or if endDate is greater thanmaxdate, endDate will be updated with themaxdate.
Range span
The days span between ranges can be limited in order to avoid excess or less days selection towards the required days in a range. In this, the minimum and maximum span allowed within the date range can be customized by the 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.
In the following sample, the range selection should be greater than 3 days and less than 8 days else it will not set.
import { DateRangePicker } from '@syncfusion/ej2-calendars';
let today: Date = new Date();
let currentYear: number = today.getFullYear();
let currentMonth: number = today.getMonth();
let currentDay: number = today.getDate();
// creates a daterangepicker with min max property
let daterangeObject: DateRangePicker = new DateRangePicker({
//sets the minimum number of days
minDays: 4,
//sets the maximum number of days
maxDays: 7,
placeholder:"Select a Range"
});
daterangeObject.appendTo('#element');<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 DateRangePicker control</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-lists/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-calendars/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<input id='element' type="text" />
</div>
</body>
</html>Strict mode
The DateRangePicker provides an option to limit entering only the valid date. With strict mode, only the valid date can be set. If any invalid range is specified, the date range value resets to previous value. This restriction can be availed by enabling the strictMode property as true.
import { DateRangePicker } from '@syncfusion/ej2-calendars';
// creates a daterangepicker in strict mode
let daterangeObject: DateRangePicker = new DateRangePicker({
//enabling strict mode
strictMode: true, placeholder:"Select a Range"
});
daterangeObject.appendTo('#element');<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 DateRangePicker control</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-lists/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-calendars/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<input id='element' type="text" />
</div>
</body>
</html>