Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 03-10-2002, 12:36 PM
Luke Luke is offline
Senior Member
 
Join Date: Feb 2001
Posts: 359
Default How to check a correct date?

Hi to all,


I would like to write a function that inform me if the date typed by the user is correct or not. Format date is dd/mm/yyyy. So the first 2 numbers between 1 and 31, then '/', 3 and 4 number between 1 and 12 and the last 4 number for example between 1900 and 2010.
Any ideas to develope this function that gives to me if the date insered ic correct??

Thanks to all for the collaboration,
Luke
Reply With Quote
  #2  
Old 03-10-2002, 03:13 PM
HTML HTML is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,977
Default RE: How to check a correct date?

Hello, Luke

This will work for your need.

{---------------------------------------------------------}
function IsDateValid( ADateStr:string ):boolean;
begin
if Length(ADateStr)=10 then {length must be 10}

{checks that both slashes should be in correct places}
if (Copy(ADateStr,3,1)='/') AND (Copy(ADateStr,6,1)='/') then
try
StrToDate(ADateStr);
Result := True;
except
Result := False;
end; {try}

end;
{---------------------------------------------------------}



But just a note, if you use a string that will have letters the debugger will throw a exception. But not during runtime becuase of try block.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:52 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.