Entering Invalid Dates Just Got Better in Angular Apps
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (179).NET Core  (28).NET MAUI  (214)Angular  (110)ASP.NET  (51)ASP.NET Core  (83)ASP.NET MVC  (90)Azure  (41)Black Friday Deal  (1)Blazor  (226)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (69)Flutter  (133)JavaScript  (226)Microsoft  (120)PDF  (81)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (943)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (163)Xamarin  (160)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (158)Chart  (140)Chart of the week  (54)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (70)Development  (659)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (14)Excel  (43)Extensions  (23)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (512)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (14)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (113)UI  (393)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (615)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Entering Invalid Dates Is Not Possible Anymore in Angular Apps

Entering Invalid Dates Just Got Better in Angular Apps

How many times have you seen people making mistakes while entering dates? It is mostly due to the lack of clarity in the UI as to format.

The Syncfusion Angular DatePicker provides mask support to improve the user experience while entering dates by providing month, day, and year fields. These help users enter valid dates in the correct format and also keep them from entering invalid input dates.

For example, if you enter 6 (June) in the month field, then you can enter only dates from 1 to 30 in the day field. The component won’t allow you to enter values above 30.

In this blog post, we will see how to get started with the Angular DatePicker and enable mask support to easily handle dates in it.

Setup Angular environment

Follow these steps to set the angular CLI projects using the Angular CLI tool.

  1. First, install the latest version of Angular CLI locally in your application using the following command.
    npm install -g @angular/cli@latest

    Note: If you would like to follow and run the application in Angular 8 to 11, you need to replace the CLI command version number with the corresponding Angular version number.

    npm install -g @angular/cli@<CLI VERSION>
  1. Now, create a new Angular project by using the ng new command and navigate to the created project folder. Here, I am naming this Angular project angular-datepicker-mask.
    ng new < angular-datepicker-mask>
    cd < angular-datepicker-mask>
  1. Then, use the following command to install the ej2-angular-calendars npm package locally in the application.
    npm install @syncfusion/ej2-angular-calendars --save

Adding Angular DatePicker

Follow these steps to include the Angular DatePicker component in your application.

  1. The following CSS files are available in the ../node_modules/@syncfusion package folder. This can be referenced in the [src/styles.css] file using the following code.

    @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-splitbuttons/styles/material.css';
    @import '../node_modules/@syncfusion/ej2-calendars/styles/material.css';
    @import '../node_modules/@syncfusion/ej2-angular-calendars/styles/material.css';
  1. Then, import the DatePicker module into the Angular application (app.module.ts) from the ej2-angular-calendars package. Refer to the following code example.

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { DatePickerModule } from '@syncfusion/ej2-angular-calendars';
    import { AppComponent } from './app.component';
      declarations: [
      imports: [
      providers: [],
      bootstrap: [AppComponent]
    export class AppModule { }
  1. Next, define the Angular DatePicker within the component.html file mapped against the templateUrl option in the app.component.ts file.

  1. Now, run the application with the ng serve command and it will display a DatePicker on the browser like in the following screenshot.

    Angular DatePicker
    Angular DatePicker

Thus, we have included the DatePicker component in our Angular application. Let’s see how to enable the mask support to easily handle dates in the Angular DatePicker.

Enable mask input

You can use the enableMask property to enable the built-in date format masking. By default, the mask pattern is formatted based on the current culture.

To use mask support, inject the MaskedDateTimeService module into the Angular DatePicker.

Refer to the following code example.


import { Component } from '@angular/core';
import { MaskedDateTimeService } from '@syncfusion/ej2-angular-calendars';  

  selector: 'app-root',
  styleUrls: ['./app.component.scss'],
  templateUrl: './app.component.html',
  providers: [MaskedDateTimeService],
export class AppComponent {
  title = 'angular-datepicker-mask';
  public enableMaskSupport: boolean = true;


<div style="width: 300px; margin: auto;">
  <ejs-datepicker [enableMask]="enableMaskSupport">
Enabling Mask Input in Angular DatePicker
Enabling Mask Input in Angular DatePicker

Configure mask placeholder

You can change the mask placeholder value through the maskPlaceholder property. By default, it takes the full name of date and time coordinates such as day, month, year, and hour.

Note: While changing to a culture other than English, ensure that the locale text for the concerned culture is loaded through the load method of the L10n class for mask placeholder values.

Refer to the following code example in which we have customized the default month/day/year format to MM/dd/yyyy.


public maskPlaceholderValue: Object = {day: 'dd', month: 'MM', year: 'yyyy'}


<div style="width: 300px; margin: auto;">
  <ejs-datepicker [enableMask]="enableMaskSupport" [maskPlaceholder]="maskPlaceholderValue">
Configuring Mask Placeholder in Angular DatePicker
Configuring Mask Placeholder in Angular DatePicker

Keyboard accessibility

Also, the mask support in Angular DatePicker provides keyboard interaction to easily handle dates. With this, you can change the values, increments, and decrements of the selected portions of date and time coordinates using the Up and Down arrow keys.

You can also use the Right and Left arrow keys to navigate from one segment to another.

Keyboard Accessibility in Angular DatePicker
Keyboard Accessibility in Angular DatePicker


For more information, refer to the Mask support in Angular DatePicker demos on the web and the GitHub repository.


Thanks for reading! In this blog post, we have seen how to enable the masking feature to easily handle dates in our Angular DatePicker component. This feature helps users enter valid dates in the correct format and avoid data input errors.

Also, try our Angular DatePicker component by downloading a free 30-day trial. Feel free to have a look at the documentation to explore other available features.

If you have any questions, please let us know in the comments section below. You can also contact us through our support forum or Direct-Trac. We are always happy to assist you!

Related blogs


Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed