How can i have my DateTimePicker field mandatory ?

Hi,

I have a date field like this : 

But when i erase the default date, i obtain this : 

And when i submit my form, the result is Something like this : "01/01/0001 00:00:00"
My problem is that i want to make this date field mandatory.

I tried to make [required] in the model, but it does not work.
I tried to add this : <span asp-validation-for="dtDateDemarrage" class="text-danger"></span>
But it Don't works either.

Can you help me please ?
Thank's by advance.
FreD.

1 Reply

DL Deepa Loganathan Syncfusion Team September 14, 2018 08:39 AM UTC

Hi Frederic,   
  
Thanks for contacting Syncfusion Support.   
  
Query1: When I submit my form, the result is Something like this: "01/01/0001 00:00:00"   
  
Answer: The date value will become “01/01/0001 00:00:00” because of defining the model value as DateTime type. This is because the DateTime type can’t accept null value, hence it will automatically change to the min value (01/01/0001 00:00:00) instead of null.  So please define the model variable as a nullabe DateTime type by adding a question mark next to the type of the variable in the model class as shown in the below code snippet.   
  
public class ModelValues   
        {   
            [Required(ErrorMessage = "Valid Date is Required")]   
            public DateTime? DateValue { getset; }   
        }   
 
  
Query2: I tried to add this: <span asp-validation-for="dtDateDemarrage" class="text-danger"></span> But it Don't works either.   
   
Answer: We suspect the cause for this issue may be the value of asp-validation-for attribute might be misspelled or a wrong value instead of assigning the model variable name. So please ensure that the model variable name assigned for the DatePicker component is assigned as the value for the asp-validation-for attribute as shown in the below code snippet.   
  
<ej-date-picker id="datepick" ej-for="@Model.DateValue" ></ej-date-picker>   
<span asp-validation-for="DateValue" class="text-danger"></span>   
 
  
For your convenience, we have attached a simple sample for validating the DatePicker component in the below link. Please check it.   
  
To know more about validating the DatePicker component at server side, please check the below documentation link.   
  
   
Regards,   
Deepa L.  



Loader.
Up arrow icon