Recent

Author Topic: Date Validation problem  (Read 1923 times)

ranlaborde

  • New Member
  • *
  • Posts: 11
Date Validation problem
« on: July 22, 2017, 03:54:35 pm »
I have written some code to handle date separator and order differences among users with different setups on their local machines.  The code works on two different Win10 machines with different date configurations ( one has 'm-d-y' the other 'd-m-y'.  However, I am running into a problem on Mac OS.  When an invalid date is entered focus is returned to two textboxes rather than the offending textbox.

Code: Pascal  [Select][+][-]
  1. unit uvalidationtest;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   DateUtils, LCLType;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     btnExit: TButton;
  17.     txtDOB: TEdit;
  18.     Label2: TLabel;
  19.     txtDate: TEdit;
  20.     Label1: TLabel;
  21.     procedure btnExitClick(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure txtDateExit(Sender: TObject);
  24.     function CheckSeparators(ldate:String):String;
  25.     function CheckIfDate(T: TControl):boolean;
  26.     procedure txtDOBExit(Sender: TObject);
  27.   private
  28.     { private declarations }
  29.   public
  30.     { public declarations }
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.btnExitClick(Sender: TObject);
  43. begin
  44.   Application.Terminate;
  45. end;
  46.  
  47. procedure TForm1.FormCreate(Sender: TObject);
  48. var
  49.   lhint: String;
  50. begin
  51.   lhint:= 'Enter in ' + LowerCase(FormatSettings.ShortDateFormat) + ' order';
  52.   txtDate.Hint:= lhint;
  53.   txtDate.ShowHint:= True;
  54.   txtDOB.Hint:= lhint;
  55.   txtDOB.ShowHint:= True;
  56. end;
  57.  
  58. procedure TForm1.txtDateExit(Sender: TObject);
  59. begin
  60.   if Not CheckIfDate(txtDate) then
  61.       txtDate.SetFocus;
  62. end;
  63.  
  64. procedure TForm1.txtDOBExit(Sender: TObject);
  65. begin
  66.   if Not CheckIfDate(txtDOB) then
  67.       txtDOB.SetFocus;
  68. end;
  69.  
  70. function TForm1.CheckSeparators(ldate:String):String;
  71. var
  72.   ds: String;
  73.   ldateseparator: String;
  74. begin
  75.   ds:= FormatSettings.DateSeparator;
  76.   if pos(ds,ldate) > 0 then
  77.     begin
  78.       result:= ldate;
  79.     end
  80.   else
  81.     begin
  82.       if pos('/', ldate) > 0 then
  83.         ldateseparator:= '/'
  84.       else if pos('-', ldate) > 0 then
  85.         ldateseparator:= '-'
  86.       else
  87.         ldateseparator:= '.';
  88.       result:= StringReplace(ldate, ldateseparator, ds, [rfReplaceAll]);
  89.     end;
  90. end;
  91. function TForm1.CheckIfDate(T: TControl):boolean;
  92. var
  93.   checkdate: String;
  94.   ldatereturn: TDateTime;
  95.   lmsg: String;
  96. begin
  97.  checkdate:= T.Caption;
  98.  checkdate:= CheckSeparators(checkdate);
  99.  T.Caption:= checkdate;
  100.   if TryStrToDate(checkDate, ldatereturn) then
  101.       result:= True
  102.   else
  103.       begin
  104.         lmsg:= 'An improper date was entered. Enter in ' + LowerCase(FormatSettings.ShortDateFormat) + ' order';
  105.         Application.MessageBox(pchar(lmsg), 'Invalid Date...', MB_ICONINFORMATION);
  106.         result:= False;
  107.       end;
  108. end;
  109.  
  110. end.
  111.  

How to I handle this problem?  Attached is the sample application.

Dzandaa

  • Full Member
  • ***
  • Posts: 248
  • From C# to Lazarus
Re: Date Validation problem
« Reply #1 on: November 09, 2022, 04:21:26 pm »
Hi,

Why Don't you use a TDateEdit control? (in Misc)

B->
Dzandaa

 

TinyPortal © 2005-2018