Range restriction in Vue Daterangepicker component
11 Jun 20248 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.
In the following sample, you can select a range from 15th day of this month to 15th day of next month.
<template>
<div id="app">
<div class='wrapper'>
<ejs-daterangepicker :min="minDate" :max="maxDate" :placeholder="waterMark"></ejs-daterangepicker>
</div>
</div>
</template>
<script setup>
import { DateRangePickerComponent as EjsDaterangepicker } from '@syncfusion/ej2-vue-calendars';
const waterMark = 'Select a Range';
const minDate = new Date(new Date().getFullYear(), new Date().getMonth(), 15);
const maxDate = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 15);
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import "../node_modules/@syncfusion/ej2-vue-calendars/styles/material.css";
.wrapper {
max-width: 250px;
margin: 0 auto;
}
</style><template>
<div id="app">
<div class='wrapper'>
<ejs-daterangepicker :min="minDate" :max="maxDate" :placeholder="waterMark"></ejs-daterangepicker>
</div>
</div>
</template>
<script>
import { DateRangePickerComponent } from '@syncfusion/ej2-vue-calendars';
export default {
name: "App",
components: {
"ejs-daterangepicker": DateRangePickerComponent
},
data() {
return {
waterMark: 'Select a Range',
minDate: new Date(new Date().getFullYear(), new Date().getMonth(), 15),
maxDate: new Date(new Date().getFullYear(), new Date().getMonth() + 1, 15)
}
}
}
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import "../node_modules/@syncfusion/ej2-vue-calendars/styles/material.css";
.wrapper {
max-width: 250px;
margin: 0 auto;
}
</style>If the value of
minormaxproperty is changed through code behind, update thestart dateandend dateproperties to set within the range. Or else , if thestartandenddate is out of 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
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.
In the following sample, the range selection should be greater than 3 days and less than 8 days else it will not set.
<template>
<div id="app">
<div class='wrapper'>
<ejs-daterangepicker :minDays="min" :maxDays="max" :placeholder="waterMark"></ejs-daterangepicker>
</div>
</div>
</template>
<script setup>
import { DateRangePickerComponent as EjsDaterangepicker } from '@syncfusion/ej2-vue-calendars';
const waterMark = 'Select a Range';
const min = 4;
const max = 8;
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import "../node_modules/@syncfusion/ej2-vue-calendars/styles/material.css";
.wrapper {
max-width: 250px;
margin: 0 auto;
}
</style><template>
<div id="app">
<div class='wrapper'>
<ejs-daterangepicker :minDays="min" :maxDays="max" :placeholder="waterMark"></ejs-daterangepicker>
</div>
</div>
</template>
<script>
import { DateRangePickerComponent } from '@syncfusion/ej2-vue-calendars';
export default {
name: "App",
components: {
"ejs-daterangepicker": DateRangePickerComponent
},
data() {
return {
waterMark: 'Select a Range',
min: 4,
max: 8
}
}
}
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import "../node_modules/@syncfusion/ej2-vue-calendars/styles/material.css";
.wrapper {
max-width: 250px;
margin: 0 auto;
}
</style>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.
<template>
<div id="app">
<div class='wrapper'>
<ejs-daterangepicker :strictMode="mode" :placeholder="waterMark"></ejs-daterangepicker>
</div>
</div>
</template>
<script setup>
import { DateRangePickerComponent as EjsDaterangepicker } from '@syncfusion/ej2-vue-calendars';
const waterMark = 'Select a Range';
const mode = true;
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import "../node_modules/@syncfusion/ej2-vue-calendars/styles/material.css";
.wrapper {
max-width: 250px;
margin: 0 auto;
}
</style><template>
<div id="app">
<div class='wrapper'>
<ejs-daterangepicker :strictMode="mode" :placeholder="waterMark"></ejs-daterangepicker>
</div>
</div>
</template>
<script>
import { DateRangePickerComponent } from '@syncfusion/ej2-vue-calendars';
export default {
name: "App",
components: {
"ejs-daterangepicker": DateRangePickerComponent
},
data() {
return {
waterMark: 'Select a Range',
mode: true
}
}
}
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import "../node_modules/@syncfusion/ej2-vue-calendars/styles/material.css";
.wrapper {
max-width: 250px;
margin: 0 auto;
}
</style>