unit uvalidationtest;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
DateUtils, LCLType;
type
{ TForm1 }
TForm1 = class(TForm)
btnExit: TButton;
txtDOB: TEdit;
Label2: TLabel;
txtDate: TEdit;
Label1: TLabel;
procedure btnExitClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure txtDateExit(Sender: TObject);
function CheckSeparators(ldate:String):String;
function CheckIfDate(T: TControl):boolean;
procedure txtDOBExit(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.btnExitClick(Sender: TObject);
begin
Application.Terminate;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
lhint: String;
begin
lhint:= 'Enter in ' + LowerCase(FormatSettings.ShortDateFormat) + ' order';
txtDate.Hint:= lhint;
txtDate.ShowHint:= True;
txtDOB.Hint:= lhint;
txtDOB.ShowHint:= True;
end;
procedure TForm1.txtDateExit(Sender: TObject);
begin
if Not CheckIfDate(txtDate) then
txtDate.SetFocus;
end;
procedure TForm1.txtDOBExit(Sender: TObject);
begin
if Not CheckIfDate(txtDOB) then
txtDOB.SetFocus;
end;
function TForm1.CheckSeparators(ldate:String):String;
var
ds: String;
ldateseparator: String;
begin
ds:= FormatSettings.DateSeparator;
if pos(ds,ldate) > 0 then
begin
result:= ldate;
end
else
begin
if pos('/', ldate) > 0 then
ldateseparator:= '/'
else if pos('-', ldate) > 0 then
ldateseparator:= '-'
else
ldateseparator:= '.';
result:= StringReplace(ldate, ldateseparator, ds, [rfReplaceAll]);
end;
end;
function TForm1.CheckIfDate(T: TControl):boolean;
var
checkdate: String;
ldatereturn: TDateTime;
lmsg: String;
begin
checkdate:= T.Caption;
checkdate:= CheckSeparators(checkdate);
T.Caption:= checkdate;
if TryStrToDate(checkDate, ldatereturn) then
result:= True
else
begin
lmsg:= 'An improper date was entered. Enter in ' + LowerCase(FormatSettings.ShortDateFormat) + ' order';
Application.MessageBox(pchar(lmsg), 'Invalid Date...', MB_ICONINFORMATION);
result:= False;
end;
end;
end.