Strict mode in Angular Datepicker component

27 Sep 20234 minutes to read

The strictModeis an act,that allows the user to enter only the valid date within the specified min/max range in textbox.
If the date is invalid, then the component will stay with the previous value.
Else, if the date is out of range, then the component will set the date to the min/max date.

The following example demonstrates the DatePicker in strictMode with min/max range of 5th to 25th in a month of May.
Here, it allows to enter only the valid date within the specified range. If you are trying to enter the out-of-range value as like 28th of May, then the value will set to the max date of 25th May. Since the value 28th is greater than to max value of 25th.
Or else if you are trying to enter the invalid date, then the value will stay with the previous value.

The following example demonstrates the DatePicker with strictMode true.

import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    template: `<ejs-datepicker strictMode='true' format='dd/MM/yyyy' placeholder='Enter date' [value]='dateValue' [min]='minDate' [max]='maxDate'></ejs-datepicker>`
})
export class AppComponent {
    public dateValue: Date = new Date("5/28/2018");
    public minDate : Date = new Date ("5/5/2018");
    public maxDate : Date = new Date ("5/25/2018");
    constructor() {
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { DatePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';


/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        DatePickerModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

By default, the DatePicker act in strictMode false state, that allows to enter the invalid or out-of-range date in textbox.

If the date is out-of-range or invalid, then the model value will be set to out of range date value or null respectively with highlighted error class to indicates the date is out of range or invalid.

The following example demonstrates the strictMode as false. Here, it allows to enter the valid or invalid value in textbox.
If you are entering out-of-range or invalid date value, then the model value will be set to out of range date value or null respectively with highlighted error class to indicates the date is out of range or invalid.

import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    template:  `<ejs-datepicker  format='dd/MM/yyyy' placeholder='Enter date' [value]='dateValue' [min]='minDate' [max]='maxDate'></ejs-datepicker>`
})
export class AppComponent {
    public dateValue: Date = new Date("5/28/2018");
    public minDate : Date = new Date ("5/5/2018");
    public maxDate : Date = new Date ("5/25/2018");
    constructor() {
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { DatePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';


/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        DatePickerModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

If the value of min or max properties changed through code behind, then you have to update the value property to set within the range.