Validation in Windows Forms DateTimePicker (SfDateTimeEdit)

18 Nov 20183 minutes to read

The SfDateTimeEdit control validates the DateTime value when the Enter key is pressed, when the control loses its focus, or when a date is picked from the drop-down calendar.

Validation reset option

The ValidationOption property determines how the value changes when the validation fails. If the validation fails, the value will reset to the previous date-time Value or MinValue or MaxValue. If the validating event is not handled, then the validation will be done based on the ValidationOption of the SfDateTimeEdit.

The validation results can be obtained based on the input provided to Value or DateTimeText of the SfDateTimeEdit. If the provided input has invalid date-time format or value that meets minimum or maximum value constraint, then the validation results will be failed.

  • Reset: A control that maintains the previous value before validating. If the validation fails, then the value will be reset to the previous value.

  • MinValue: Resets the value to MinValue, when the validation fails.

  • MaxValue: Resets the value to MaxValue, when the validation fails.

using Syncfusion.WinForms.Input;

SfDateTimeEdit dateTimeEdit = new SfDateTimeEdit();

this.Controls.Add(dateTimeEdit);

dateTimeEdit.Value = new DateTime(2018, 2, 1);

dateTimeEdit.DateTimeEditingMode = DateTimeEditingMode.Default;

// On Validation failed the value will be reset with MinValue.
dateTimeEdit.ValidationOption = ValidationResetOption.MinValue;
Imports Syncfusion.WinForms.Input

Dim dateTimeEdit As SfDateTimeEdit = New SfDateTimeEdit

Me.Controls.Add(dateTimeEdit)

dateTimeEdit.Value = New DateTime(2018, 2, 1)

dateTimeEdit.DateTimeEditingMode = DateTimeEditingMode.Default

' On Validation failed the value will be reset with MinValue.

dateTimeEdit.ValidationOption = ValidationResetOption.MinValue

The given value can be treated as a date/time value. It can be validated based on the DateTime format with culture. The following error indicating image will be shown, when the validation test is failed.

Validation support

Handle validation

The ValidatingEventArgs provides data for the Validating event of the SfDateTimeEdit control. By handling the Validating event, it is possible to find the cause for validation failure with the error message in the ValidatingEventArgs.

  • IsError: Indicates whether the entered date and time is valid or invalid.

  • ErrorMessage: Updates the cause of the error. The error may be caused due to minimum or maximum value constraint met or incorrect date time format.

using Syncfusion.WinForms.Input;

// Invoking Validating event

this.dateTimeEdit.Validating += DateTimeEdit_Validating;

private void DateTimeEdit_Validating(object sender, ValidatingEventArgs e)

{

   if (e.IsError)

   {

      MessageBox.Show(e.ErrorMessage);

   }
   
}
Imports Syncfusion.WinForms.Input

' Invoking Validating event

AddHandler Me.dateTimeEdit.Validating, AddressOf DateTimeEdit_Validating
    
    Private Sub DateTimeEdit_Validating(ByVal sender As Object, ByVal e As ValidatingEventArgs)

        If e.IsError Then

            MessageBox.Show(e.ErrorMessage)

        End If
        
    End Sub