DateInput
The DateInput component is constructed using an HTML input of type 'date' which limits user input based on pre-defined parameters. This component enables users to input a date using a text box with validation or a special date picker interface
How it works #
The DateInput component provides a user-friendly way to select or enter dates using a text box or a date picker interface. It supports two-way binding and works seamlessly with
How to use:
This demo shows the basic usage of
DateOnly? and other date types.
How to use:
- Add the
DateInputcomponent to your page. - Bind its
Valueproperty to aDateOnly?field using@bind-Value. - Read or update the bound value in your code as needed.
DateInput with two-way binding, allowing users to select a date and see the value reflected in the UI.
Entered date:
<DateInput TValue="DateOnly?" @bind-Value="@enteredDate" />
<div class="mb-3">Entered date: @enteredDate</div>
@code {
private DateOnly? enteredDate = null;
}Enable max min #
The DateInput component supports restricting selectable dates using minimum and maximum values. This is useful for scenarios where only a specific date range is valid.
How to use:
This demo shows how to restrict date selection to a specific range using
How to use:
- Set
EnableMaxMin="true"on theDateInputcomponent. - Provide
MinandMaxvalues to define the allowed date range. - Bind the
Valueproperty to aDateOnlyfield.
DateOnly values.
DateOnly:
Min date: 9/30/2025
Max date: 10/30/2026
Entered date: 1/30/2026
<div class="mb-3">
<strong>DateOnly</strong>:
</div>
<div class="mb-3">
<DateInput TValue="DateOnly" @bind-Value="@date1" EnableMaxMin="true" Min="@min1" Max="@max1" Placeholder="Enter Date" />
</div>
<div class="mb-3">Min date: @min1</div>
<div class="mb-3">Max date: @max1</div>
<div class="mb-3">Entered date: @date1</div>
@code {
private DateTime date = DateTime.Now.AddMonths(3);
private DateTime min = DateTime.Now.AddMonths(-1);
private DateTime max = DateTime.Now.AddYears(1);
private DateOnly date1, min1, max1;
protected override void OnInitialized()
{
date1 = DateOnly.FromDateTime(date);
min1 = DateOnly.FromDateTime(min);
max1 = DateOnly.FromDateTime(max);
}
}
You can also use nullable
How to use:
This demo demonstrates nullable date support with min and max constraints.
DateOnly? types with min and max restrictions. This allows the field to be empty while still enforcing the allowed date range when a value is entered.
How to use:
- Set
EnableMaxMin="true"on theDateInputcomponent. - Provide
MinandMaxvalues asDateOnly?. - Bind the
Valueproperty to aDateOnly?field.
DateOnly?:
Min date: 9/30/2025
Max date: 10/30/2026
Entered date:
<div class="mb-3">
<strong>DateOnly?</strong>:
</div>
<div class="mb-3">
<DateInput TValue="DateOnly?" @bind-Value="@date2" EnableMaxMin="true" Min="@min2" Max="@max2" Placeholder="Enter Date" />
</div>
<div class="mb-3">Min date: @min2</div>
<div class="mb-3">Max date: @max2</div>
<div class="mb-3">Entered date: @date2</div>
@code {
private DateTime date = DateTime.Now.AddMonths(3);
private DateTime min = DateTime.Now.AddMonths(-1);
private DateTime max = DateTime.Now.AddYears(1);
private DateOnly? date2, min2, max2;
protected override void OnInitialized()
{
date2 = null;
min2 = DateOnly.FromDateTime(min);
max2 = DateOnly.FromDateTime(max);
}
}
The
How to use:
This demo shows how to use
DateInput component also works with DateTime types for min and max validation. This is useful when you need to work with both date and time information.
How to use:
- Set
EnableMaxMin="true"on theDateInputcomponent. - Provide
MinandMaxvalues asDateTime. - Bind the
Valueproperty to aDateTimefield.
DateInput with DateTime values and range validation.
DateTime:
Min date: 9/30/2025 2:15:48 AM
Max date: 10/30/2026 2:15:48 AM
Entered date: 1/30/2026 2:15:48 AM
<div class="mb-3">
<strong>DateTime</strong>:
</div>
<div class="mb-3">
<DateInput TValue="DateTime" @bind-Value="@date3" EnableMaxMin="true" Min="@min3" Max="@max3" Placeholder="Enter Date" />
</div>
<div class="mb-3">Min date: @min3</div>
<div class="mb-3">Max date: @max3</div>
<div class="mb-3">Entered date: @date3</div>
@code {
private DateTime date = DateTime.Now.AddMonths(3);
private DateTime min = DateTime.Now.AddMonths(-1);
private DateTime max = DateTime.Now.AddYears(1);
private DateTime date3, min3, max3;
protected override void OnInitialized()
{
date3 = DateTime.Now.AddMonths(3);
min3 = min;
max3 = max;
}
}
Nullable
How to use:
This demo demonstrates nullable
DateTime? is also supported for scenarios where the date field can be empty but still needs to respect min and max constraints.
How to use:
- Set
EnableMaxMin="true"on theDateInputcomponent. - Provide
MinandMaxvalues asDateTime?. - Bind the
Valueproperty to aDateTime?field.
DateTime? support with min and max validation.
DateTime?:
Min date: 9/30/2025 2:15:48 AM
Max date: 10/30/2026 2:15:48 AM
Entered date:
<div class="mb-3">
<strong>DateTime?</strong>:
</div>
<div class="mb-3">
<DateInput TValue="DateTime?" @bind-Value="@date4" EnableMaxMin="true" Min="@min4" Max="@max4" Placeholder="Enter Date" />
</div>
<div class="mb-3">Min date: @min4</div>
<div class="mb-3">Max date: @max4</div>
<div class="mb-3">Entered date: @date4</div>
@code {
private DateTime date = DateTime.Now.AddMonths(3);
private DateTime min = DateTime.Now.AddMonths(-1);
private DateTime max = DateTime.Now.AddYears(1);
private DateTime? date4, min4, max4;
protected override void OnInitialized()
{
date4 = null;
min4 = min;
max4 = max;
}
}
Colors #
The DateInput component supports multiple color themes to match your application's style. You can set the
How to use:
This demo shows how to apply different color styles to the
Color property to apply different visual states such as primary, info, success, warning, or danger.
How to use:
- Add the
DateInputcomponent to your page. - Set the
Colorproperty to one of the availableTextInputColorvalues (e.g.,Primary,Success). - Bind the
Valueproperty as usual.
DateInput component.
<DateInput Class="mb-2" Color="TextInputColor.Link" @bind-Value="@value1" />
<DateInput Class="mb-2" Color="TextInputColor.Primary" @bind-Value="@value2" />
<DateInput Class="mb-2" Color="TextInputColor.Info" @bind-Value="@value3" />
<DateInput Class="mb-2" Color="TextInputColor.Success" @bind-Value="@value4" />
<DateInput Class="mb-2" Color="TextInputColor.Warning" @bind-Value="@value5" />
<DateInput Class="mb-2" Color="TextInputColor.Danger" @bind-Value="@value6" />
@code {
private DateOnly? value1 = null;
private DateOnly? value2 = null;
private DateOnly? value3 = null;
private DateOnly? value4 = null;
private DateOnly? value5 = null;
private DateOnly? value6 = null;
}Sizes #
The DateInput component can be rendered in different sizes to fit various UI requirements. Use the
How to use:
This demo demonstrates the available size options for the
Size property to select from small, normal, medium, or large input fields.
How to use:
- Add the
DateInputcomponent to your page. - Set the
Sizeproperty toDateInputSize.Small,Normal,Medium, orLarge. - Bind the
Valueproperty as needed.
DateInput component.
<DateInput Class="mb-2" Size="DateInputSize.Small" @bind-Value="@value1" />
<DateInput Class="mb-2" Size="DateInputSize.Normal" @bind-Value="@value2" />
<DateInput Class="mb-2" Size="DateInputSize.Medium" @bind-Value="@value3" />
<DateInput Class="mb-2" Size="DateInputSize.Large" @bind-Value="@value4" />
@code {
private DateOnly? value1 = null;
private DateOnly? value2 = null;
private DateOnly? value3 = null;
private DateOnly? value4 = null;
}Styles #
The DateInput component supports additional styling options, such as rounded corners. Use the
How to use:
This demo shows how to enable rounded styling for the
IsRounded property to enable a rounded appearance.
How to use:
- Add the
DateInputcomponent to your page. - Set
IsRounded="true"to apply rounded styling. - Bind the
Valueproperty as needed.
DateInput component.
<DateInput IsRounded="true" @bind-Value="@value1" />
@code {
private DateOnly? value1 = null;
}States #
The DateInput component can visually reflect different states such as hovered, focused, or loading. Use the
How to use:
This demo demonstrates how to display the
State property to control the visual state of the input.
How to use:
- Add the
DateInputcomponent to your page. - Set the
Stateproperty toTextInputState.Hovered,Focused, orLoadingas needed. - Bind the
Valueproperty as usual.
DateInput in different visual states.
<DateInput Class="mb-2" @bind-Value="@value1" />
<DateInput Class="mb-2" State="TextInputState.Hovered" @bind-Value="@value2" />
<DateInput Class="mb-2" State="TextInputState.Focused" @bind-Value="@value3" />
<DateInput Class="mb-2" State="TextInputState.Loading" @bind-Value="@value4" />
@code {
private DateOnly? value1 = null;
private DateOnly? value2 = null;
private DateOnly? value3 = null;
private DateOnly? value4 = null;
}Disabled #
The DateInput component can be disabled to prevent user interaction. Set the
How to use:
This demo shows how to render a disabled
Disabled property to true to make the input read-only.
How to use:
- Add the
DateInputcomponent to your page. - Set
Disabled="true"to disable the input. - Bind the
Valueproperty as needed.
DateInput field.
<DateInput Class="mb-2" @bind-Value="@value1" Disabled="true" />
@code {
private DateOnly? value1 = null;
}Validations #
The DateInput component integrates with Blazor's form validation system. Use it inside an
How to use:
This demo demonstrates how to use
EditForm with data annotations to provide required field validation and display error messages.
How to use:
- Place the
DateInputinside anEditFormcomponent. - Bind the
Valueproperty to a model property decorated with validation attributes (e.g.,[Required]). - Add
DataAnnotationsValidatorandValidationMessagecomponents for validation support and error display. - Handle form submission and reset as needed.
DateInput with form validation and error messages.
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms
<style>
/* validations */
.valid.modified:not([type=checkbox]) {
border-color: hsl(var(--bulma-success-h), var(--bulma-success-s), var(--bulma-success-l)) !important;
outline: 1px solid hsl(var(--bulma-success-h), var(--bulma-success-s), var(--bulma-success-l)) !important;
}
.invalid {
border-color: hsl(var(--bulma-danger-h), var(--bulma-danger-s), var(--bulma-danger-l)) !important;
outline: 1px solid hsl(var(--bulma-danger-h), var(--bulma-danger-s), var(--bulma-danger-l)) !important;
}
.validation-message {
color: hsl(var(--bulma-danger-h), var(--bulma-danger-s), var(--bulma-danger-l)) !important;
}
</style>
<EditForm EditContext="@editContext" OnValidSubmit="HandleOnValidSubmit">
<DataAnnotationsValidator />
<div class="form-group row mb-3">
<label class="col-md-2 col-form-label">Invoice Date: <span class="text-danger">*</span></label>
<div class="col-md-10">
<DateInput TValue="DateOnly?" @bind-Value="invoice.InvoiceDate" />
<ValidationMessage For="@(() => invoice.InvoiceDate)" />
</div>
</div>
<div class="form-group row mb-3">
<label class="col-md-2 col-form-label">Customer Name: <span class="text-danger">*</span></label>
<div class="col-md-10">
<TextInput BindEvent="BindEvent.OnInput" @bind-Value="invoice.CustomerName" Placeholder="Enter Customer Name" />
<ValidationMessage For="@(() => invoice.CustomerName)" />
</div>
</div>
<div class="row">
<div class="col-md-12 text-right">
<Button Type="ButtonType.Button" Color="ButtonColor.Light" Size="ButtonSize.Small" Class="float-end" @onclick="ResetForm">Reset</Button>
<Button Type="ButtonType.Submit" Color="ButtonColor.Success" Size="ButtonSize.Small" Class="float-end me-2">Submit</Button>
</div>
</div>
</EditForm>
@code {
private Invoice invoice = new();
private EditContext? editContext;
protected override void OnInitialized()
{
editContext = new EditContext(invoice);
base.OnInitialized();
}
public void HandleOnValidSubmit()
{
// additional check
if (editContext.Validate())
{
// do something
// submit the form
Console.WriteLine($"Invoice Date: {invoice.InvoiceDate}");
Console.WriteLine($"Customer Name: {invoice.CustomerName}");
}
}
private void ResetForm()
{
invoice = new();
editContext = new EditContext(invoice);
}
public class Invoice
{
[Required(ErrorMessage = "Invoice Date required.")]
public DateOnly? InvoiceDate { get; set; }
[Required(ErrorMessage = "Customer Name required.")]
public string? CustomerName { get; set; }
}
}
Alignment #
The DateInput component supports text alignment customization. Use the
How to use:
This demo shows how to control the text alignment within the
TextAlignment property to align the input text to the left, center, or right.
How to use:
- Add the
DateInputcomponent to your page. - Set the
TextAlignmentproperty toTextAlignment.Left,Center, orRightas needed. - Bind the
Valueproperty as usual.
DateInput field.
<DateInput Class="mb-2" @bind-Value="@enteredText1" />
<DateInput Class="mb-2" @bind-Value="@enteredText2" TextAlignment="TextAlignment.Left" />
<DateInput Class="mb-2" @bind-Value="@enteredText3" TextAlignment="TextAlignment.Center" />
<DateInput Class="mb-2" @bind-Value="@enteredText4" TextAlignment="TextAlignment.Right" />
@code {
private DateOnly? enteredText1 = new DateOnly(2025, 02, 25);
private DateOnly? enteredText2 = new DateOnly(2025, 02, 26);
private DateOnly? enteredText3 = new DateOnly(2025, 02, 27);
private DateOnly? enteredText4 = new DateOnly(2025, 02, 28);
}Events: ValueChanged #
The DateInput component exposes a
How to use:
This demo demonstrates how to handle the
ValueChanged event that is triggered whenever the user selects or enters a new date. This allows you to react to changes and update your application state accordingly.
How to use:
- Add the
DateInputcomponent to your page. - Set the
ValueChangedparameter to a callback method that handles the new value. - Optionally, use a button or other UI to programmatically change the date and observe the event behavior.
ValueChanged event and update the UI when the date changes.
Changed date: 10/30/2025
<DateInput TValue="DateOnly" Value="date1" ValueExpression="() => date1" ValueChanged="(value) => DateChanged(value)" />
<div class="mb-3">Changed date: @date1</div>
<Button Color="ButtonColor.Primary" Size="ButtonSize.Small" @onclick="ChangeDate"> Change Date </Button>
@code {
private DateOnly date1 = DateOnly.FromDateTime(DateTime.Now);
private void DateChanged(DateOnly dateOnly)
{
date1 = dateOnly;
}
private void ChangeDate() => date1 = DateOnly.FromDateTime(DateTime.Now.AddDays(3));
}