Search code examples
datepickerfreepascallazarus

How do I check if a TDateEdit is left blank by the user?


I'm using the TDateEdit control on Lazarus and I have set the date format to DMY. So I see some characters for the user like '__/__/____' in the date input box.

My concern is that I don't want the user to leave this field blank so I'm trying the following code as a test:

  if dtWeekEndingDate.Date = 0 Then
     begin
       Errors := true;
       showmessage ('blank');
     end;

The code seems to do what I want but I'm not sure if this is correct.

Is the above method the right way to check for an unset or unentered date? I used the .Text property but it doesn't seem to work.

Thanks!


Solution

  • Yes if you can consider 0 (zero=30.12.1899 00:00) as a 'blank' date in your application. You can see this simply with outputing 'empty' TDateEdit control:

    ShowMessage(FormatDateTime('DD MM YYYY hh:nn',DateEdit1.Date));
    

    If this date is a valid and posible entry in your case then use onChange event to set boolean that something is entered or directly check and process another property of TDateEdit control:

    DateEdit1.Text
    

    Of course you can use some nice components allready handling this kind of 'problem' as: ZVDateTimeControls