Strict Mode
19 Dec 20223 minutes to read
The strictMode is an act, that allows the user to enter only the valid date and time within the specified min/max range in textbox. If the input entered is invalid, then the control will stay with the previous value. Else, if the datetime is out of range, then the control will set the datetime to the min/max value.
The following example demonstrates the DateTimePicker in strictMode
with min/max range of 5/5/2019 2:00 AM
to 5/25/2019 2:00 AM
. Here, it allows to enter only the valid date and time within the specified range. If you are trying to enter the out-of-range value as 5/28/2019
, then the value will set the max
value as 5/25/2019 2:00 AM
, since the value 28 is greater than max
value of 25. 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 DateTimePicker with strictMode true
.
@Html.EJS().DateTimePicker("datetimepicker").StrictMode(true).Max(ViewBag.maxDate).Min(ViewBag.minDate).Value(@ViewBag.value).Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EJ2CoreSampleBrowser.Controllers
{
public class HomeController : Controller
{
public ActionResult StrictMode()
{
ViewBag.minDate= new DateTime(2019, 5, 5, 02,00,00);
ViewBag.maxDate = new DateTime(2019, 5, 25, 02, 00, 00);
ViewBag.value = new DateTime(2019, 5, 28, 02, 00, 00);
return View();
}
}
}
By default, the DateTimePicker act in strictMode false
state allows to enter the invalid or out-of-range datetime in textbox.
If the datetime is out-of-range or invalid, then the model value will be set to out of range
datetime value or null
respectively with highlighted error
class to indicate that the datetime 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 the out-of-range or invalid datetime value, then the model value will be set to out of range
datetime value or null
respectively with highlighted error
class to indicate that the datetime is out of range or invalid.
@Html.EJS().DateTimePicker("datetimepicker").Max(ViewBag.maxDate).Min(ViewBag.minDate).Value(@ViewBag.value).Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EJ2CoreSampleBrowser.Controllers
{
public class HomeController : Controller
{
public IActionResult StrictModeDisabled()
{
ViewBag.minDate= new DateTime(2017, 5, 5, 02, 00, 00);
ViewBag.maxDate = new DateTime(2017, 5, 25, 03, 00, 00);
ViewBag.value = new DateTime(2017, 5, 25, 04, 00, 00);
return View();
}
}
}
NOTE
If the value of
min
ormax
properties changed through code behind, then you have to update thevalue
property to set within the range.