Enhance date input and validation

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1950 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
jujibo
2011-09-15 08:57:15 +00:00
parent e1f2a19114
commit 6f7b2841f5
2 changed files with 41 additions and 23 deletions

View File

@@ -10,8 +10,10 @@ uses
function ReplaceChar(const s: string; ch1: char; ch2: char): string; function ReplaceChar(const s: string; ch1: char; ch2: char): string;
function CountChar(const s: string; ch: char): integer; function CountChar(const s: string; ch: char): integer;
procedure Split(const Delimiter: char; Input: string; Strings: TStrings); procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
function NormalizeDate(const Value: string; theValue: TDateTime): string; function NormalizeDate(const Value: string; theValue: TDateTime;
const theFormat: string = ''): string;
function NormalizeDateSeparator(const s: string): string; function NormalizeDateSeparator(const s: string): string;
function IsValidDateString(const Value: string): boolean;
implementation implementation
@@ -43,29 +45,47 @@ begin
Strings.DelimitedText := Input; Strings.DelimitedText := Input;
end; end;
function NormalizeDate(const Value: string; theValue: TDateTime): string; function NormalizeDate(const Value: string; theValue: TDateTime;
const theFormat: string): string;
var var
texto: string; texto: string;
i: integer; i: integer;
d, m, y: word; d, m, y: word;
ds, ms, ys: string;
aDate: TDateTime;
tokens: TStringList;
begin begin
if theValue = 0 then if theValue = 0 then
DecodeDate(Now, y, m, d) DecodeDate(Now, y, m, d)
else else
decodedate(theValue, y, m, d); decodedate(theValue, y, m, d);
// normalize date ds := IntToStr(d);
ms := IntToStr(m);
ys := IntToStr(y);
texto := Value; texto := Value;
texto:= NormalizeDateSeparator(texto); texto := NormalizeDateSeparator(texto);
//texto := replacechar(texto, '.', DateSeparator); Result := texto; // default value
//texto := replacechar(texto, '-', DateSeparator);
//texto := replacechar(texto, '/', DateSeparator);
i := countchar(texto, DateSeparator); i := countchar(texto, DateSeparator);
tokens := TStringList.Create;
case i of Split(DateSeparator, texto, tokens);
1: texto := texto + DateSeparator + IntToStr(y); if tokens.Count > 0 then
0: texto := texto + DateSeparator + IntToStr(m) + DateSeparator + IntToStr(y); begin
// for now only working date forma d/m/y
// TODO add logic to make it working with m/d/y and ISO format
// update jdbgridutils to work with this code
if tokens[0] <> '' then
ds := tokens[0];
if (tokens.Count > 1) and (tokens[1] <> '') then
ms := tokens[1];
if (tokens.Count > 2) and (tokens[2] <> '') then
ys := tokens[2];
texto := ds + DateSeparator + ms + DateSeparator + ys;
if IsValidDateString(texto) then
begin
Result:= texto;
end;
end; end;
Result := texto; tokens.Free;
end; end;
function NormalizeDateSeparator(const s: string): string; function NormalizeDateSeparator(const s: string): string;
@@ -78,5 +98,13 @@ begin
Result[i] := DateSeparator; Result[i] := DateSeparator;
end; end;
function IsValidDateString(const Value: string): boolean;
begin
if StrToDateDef(Value, MaxDateTime) = MaxDateTime then
Result := False
else
Result := True;
end;
end. end.

View File

@@ -69,7 +69,6 @@ type
procedure formatInput; procedure formatInput;
procedure setFormat(const AValue: string); procedure setFormat(const AValue: string);
procedure OnKeyPress(Sender: TObject; var key: char); procedure OnKeyPress(Sender: TObject; var key: char);
function IsValidDate(const Value: string): boolean;
public public
function isNull: boolean; function isNull: boolean;
property format: string read getFormat write setFormat; property format: string read getFormat write setFormat;
@@ -235,7 +234,7 @@ begin
if Length(myEdit.Caption) = 0 then if Length(myEdit.Caption) = 0 then
theValue := 0 theValue := 0
else else
if IsValidDate(bufCaption) then if IsValidDateString(bufCaption) then
begin begin
theValue := StrToDate(bufCaption); theValue := StrToDate(bufCaption);
myEdit.Caption := bufCaption; myEdit.Caption := bufCaption;
@@ -267,15 +266,6 @@ begin
Key := #0; Key := #0;
end; end;
function TJDBDateCtrl.IsValidDate(const Value: string): boolean;
begin
// comprobar que la fecha es valida
if StrToDateDef(Value, MaxDateTime) = MaxDateTime then
Result := False
else
Result := True;
end;
function TJDBDateCtrl.isNull: boolean; function TJDBDateCtrl.isNull: boolean;
begin begin
Result := theValue = 0; Result := theValue = 0;