星期四, 十一月 05, 2009

Delphi - Check Date 範例

為避免使用者填寫日期時,亂填寫,所以我們在存入資料庫前會先做判斷該User所填寫的值是否符合規則...


function CheckDate(datestr : String):String;
var
ErrorMsg,t_year,t_month, t_day : String;
begin
t_year := copy(datestr,1,4); //yeah
t_month := copy(datestr,5,2); //month
t_day := copy(datestr,7,2); //day

if copy(t_year,1,1) <> '2' then
ErrorMsg := ErrorMsg+'西元年格式錯誤!!'+#13#10;
if (StrToInt(t_month) > 12) or (StrToInt(t_month) < 1) then
ErrorMsg := ErrorMsg+'月份格式錯誤!!'+#13#10;
if (StrToInt(t_day) > 31) or (StrToInt(t_day) < 1) then
ErrorMsg := ErrorMsg+'日期格式錯誤!!'+#13#10;
result := ErrorMsg;
end;

0 意見: