What is a good way to validate date?

Hello,

   I have a database app using oracle and there are about ten users all of them enter data. Right now I have very limited data entry mostly mask edits but a friend gave me this chuck of code and I am unsure of how to use it.
  Also I just want to have one pice of code for validation that I share for all my edit boxes, then just pass the values to it and test. I would like to prevent characters like :'>?/}\@#$ ect...

Here is the code:

procedure TfrmVol.validate;
Var
 sTmp : string;
 p : integer;
Begin
  sTmp := memSummary.Lines.Text;
  sTmp := '';
  for p := 0 to memSummary.Lines.Count - 1 do begin
      sTmp := sTmp + memSummary.Lines[p] + #13#10;
  end;
  p  := Pos( #$D, sTmp);
  while P > 0 do begin
    delete( sTmp, p, 1 );
    p := Pos( #$D, sTmp);
  end;

  while Pos( '''', sTmp) > 0 do begin
    sTmp[Pos('''', sTmp)] := '"';
  end;
end;

Thanx,
MrError
MrErrorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RadlerCommented:
I'm confuse,

function Validate : boolean;
var Dt : DateTime;
begin
try
  Dt:=StrToDate( STmp );
  Result:=True;
except
  Result:=False;
end;
end;

T++, Radler
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbarryCommented:
You would have to either
1) group your TEdit validators by type and assign them individually to the handlers via the OnChange or OnValidate events (e.g. All date edits would go to a ValidDate routine such as above example) or
2) have one validation routine smart enough to recognize the components and their respective type requirements as follow:

procedure ValidateEdits(Sender:TObject);
begin
  case Sender of
   StartDateEdit,
   EndDateEdit,
   SomeOtherDateEdit:StrToDate(TEdit(Sender).Text);
   ANumberEdit,
   AnotherNumberEdit:StrToInt(TEdit(Sender).Text);
// other types follow in fashion
  end;
end;

The type conversion exceptions will be raised when necessary (you may have to decide whether or not '' is a valid substitute for zero and filter accordingly because it won't convert).
0
MrErrorAuthor Commented:
Radler, Ibarry

  That is not exactly what I am looking for. I want to prevent users from entering in bad data to more than just date fields. I want to prevent users from entering in a firstname of D$avid or A last name of O'Conner or a address of ^*(0 th street.

   So I will have to procedure one for dates one for all others. The data thig is good, But I need somthing to prevent ^%$^()!@# characters Mostly.

  Lets leave the question open for alot of debate the more response the better.

MrError
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

kretzschmarCommented:
hi mrError,

why not use a maskedit?

if it database-related:
each TField provides a editmask-property, which can set at runtime

example:

table1.FieldByName('AName').EditMask := SomeMaskString;

meikl
0
jbarryCommented:
Under the section entitled "Other types"
if the result is to be be of type String then insert the following

for Slot := 1 to length(AString)do
  if AString[Slot] in InvalidCharSet then
  raise(EInvalidInput.Create(AString[ASlot]+' is an invalid character'));

You couldn't figure that out yourself?
0
nex1999Commented:
ibarry,

  I am just learning to program so I was interested in as many ideas as possible. I have some code that I use and my friend gave some of his code but does it hurt to ask for more?

  I guess your idea is fine but I was hoping for more than three people commenting. It is better to send comment and if its a good idea let the person accept it as an answer because when you propose an answer nobody else looks at the question.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.