Board index » delphi » Validating Dates

Validating Dates

What is the value of an blank date and how do I set this?  How can I
validate a date making sure it is an actual date before checking to see if
it is a date my system can use?  I am getting an Error with :

Exeption EVariantError with message 'Invalid type conversion' when trying to
enter an invalid date i.e. 2/30/01

Thanks,

Matt

 

Re:Validating Dates


"Matthew Tilles" <westcomp...@worldnet.att.net> wrote in
<96c5em$c...@bornews.inprise.com>:

Quote
>What is the value of an blank date and how do I set this?

Matt,

If you mean during data entry (like in a TDBEdit control), try
setting the Text property of the control to '' (i.e.
MyDateControl.Text := ''), or use the Clear method
(MyDateControl.Clear).

Quote
>How can
>I validate a date making sure it is an actual date before checking
>to see if it is a date my system can use?  I am getting an Error
>with :

>Exeption EVariantError with message 'Invalid type conversion' when
>trying to enter an invalid date i.e. 2/30/01

Use the exception to do the validation:

  try
    // Perform validation here.
    // If the date is invalid, the exception will be raised and
    // the code below will be executed.  If the date is valid,
    // the code in the "except" block below will not be executed.
  except
    on E : EVariantError do
      // Put code here to handle an invalid date.
  end; // try

HTH,

Chris.
---------------

Re:Validating Dates


Quote
"Matthew Tilles" <westcomp...@worldnet.att.net> wrote in message

news:96c5em$c573@bornews.inprise.com...

Quote
> What is the value of an blank date and how do I set this?  How can I
> validate a date making sure it is an actual date before checking to see if
> it is a date my system can use?  I am getting an Error with :

Here are two procedures that I use quite often. The first is a KeyPress
procedure for a TEdit. It allows you to enter dates and automatically
inserts slashes (/) for you. It also uses the old Paradox style of pressing
the space bar to put in the current day, month and/or year. For instance, if
you want to enter yesterday's date, you would hit space for the current
month, then the numbers for yesterdays day of month, then space again for
the current year. I find it simplifies my date entry. The second routine is
a date validation function. Nothing special but it helps.

HTH
Woody

procedure Form1.DateKeyPress(Sender: TObject; var Key: Char);
const
  Zero: char = '0';
var
  s: string;
  x: integer;

begin
  s := copy(TEdit(Sender).Text,1,TEdit(Sender).SelStart);
  x := length(s);
  case Key of
   ' ': begin
       if x = 0 then
         s := FormatDateTime('mm/',Now)
       else if x in [2..3] then
         if s[x] = '/' then begin
           if x = 2 then s := Zero + s;
           s := s + FormatDateTime('dd/',Now)
         end
         else s := s + FormatDateTime('/dd/',Now)
       else if x in [5..6] then begin
         if s[x] = '/' then begin
           if x = 5 then insert(Zero,s,4);
           s := s + FormatDateTime('yyyy',Now)
         end
         else if x = 5 then
           s := s + FormatDateTime('/yyyy',Now);
         PostMessage(handle, WM_KeyDown, VK_TAB, 0);
       end;
       TEdit(Sender).Text := s;
       Key := #0;
       TEdit(Sender).SelStart := length(s);
      end;
    '/': begin
       if s[x] <> '/' then begin
         if x = 1 then s := Zero + s + '/'
         else if x = 4 then begin
           insert(Zero, s, 4);
           s := s + '/';
         end;
       end;
       TEdit(Sender).Text := s;
       Key := #0;
       TEdit(Sender).SelStart := length(s);
     end;
    '0'..'9': begin
        if x in [2,5] then
          s := s + '/' + Key
        else if (x = 0) and (Key in ['2'..'9']) then
          s := Zero + Key
        else if (x = 3) and (Key in ['4'..'9']) then
          s := s + Zero + Key
        else s := s + Key;
        TEdit(Sender).Text := s;
        Key := #0;
        TEdit(Sender).SelStart := length(s);
      end;
   chr(13): begin
       PostMessage(handle, WM_KeyDown, VK_TAB, 0);
       Key := #0;
     end;
  end;
end;

function IsValidDate(s: string): boolean;
var
  tmpDate: TDateTime;
begin
  if length(trim(s)) = 0 then
  begin
    result := true;
    exit;
  end;
  try
    tmpDate := StrToDate(s);
    result := true;
  except
    result := false;
  end;
end;

Re:Validating Dates


Quote
In article <96c5em$c...@bornews.inprise.com>, Matthew Tilles wrote:
> What is the value of an blank date and how do I set this?  

There is no "blank date", all values you can assign to a TDatetime refer to
some date since a TDatetime is just a count of days from a reference date.
If you want to "blank" a database date field use its Clear method to set it to
NULL.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Other Threads