Board index » delphi » Validating Date Field Works but Small Problem

Validating Date Field Works but Small Problem

HI:

  Code below, fired from TField->OnSetText works fine as long as there
is  date in field. If no date in field and erroneous date entered,
Sender->FocusControl() returns focus to the offending control but the
mask 99/99/9999 is lost and you can't enter a date. As a matter of fact,
all you can enter is one number and of course the mask does not display.
You cannot reset the offending text as that fires the validate again.
Not so if the field had a date edited to an erroneous date.

Why would this be and how to work around it? Thanks.

                                                            Best regards

//---------------------------------------------------------------------------
void __fastcall TInfoDM::ValidateDateField(TField *Sender,
                         const AnsiString Text)
{
   if (Trim(Text) == "/  /")
     Sender->Clear();
   else
     {
      try
        {
         StrToDate(Text);
         Sender->AsString = Text;
        }
      catch(EConvertError *E)
        {

         String DisplayMessage = Sender->DisplayText + " is an invalid
date";
         Application->MessageBox(DisplayMessage.c_str(),
                                 "Validate Date Field",
                                 MB_OK | MB_ICONSTOP);
         Sender->FocusControl();
        }
     }

Quote
}

 

Re:Validating Date Field Works but Small Problem


Funny looking Delphi! That's not that C stuff is it?
Quote
"Robert F. Tulloch" wrote:
> HI:

>   Code below, fired from TField->OnSetText works fine as long as there
> is  date in field. If no date in field and erroneous date entered,
> Sender->FocusControl() returns focus to the offending control but the
> mask 99/99/9999 is lost and you can't enter a date. As a matter of fact,
> all you can enter is one number and of course the mask does not display.
> You cannot reset the offending text as that fires the validate again.
> Not so if the field had a date edited to an erroneous date.

> Why would this be and how to work around it? Thanks.

>                                                             Best regards

> //---------------------------------------------------------------------------
> void __fastcall TInfoDM::ValidateDateField(TField *Sender,
>                          const AnsiString Text)
> {
>    if (Trim(Text) == "/  /")
>      Sender->Clear();
>    else
>      {
>       try
>         {
>          StrToDate(Text);
>          Sender->AsString = Text;
>         }
>       catch(EConvertError *E)
>         {

>          String DisplayMessage = Sender->DisplayText + " is an invalid
> date";
>          Application->MessageBox(DisplayMessage.c_str(),
>                                  "Validate Date Field",
>                                  MB_OK | MB_ICONSTOP);
>          Sender->FocusControl();
>         }
>      }
> }

Other Threads