Link to home
Start Free TrialLog in
Avatar of MrError
MrError

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Radler
Radler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbarry
jbarry

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).
Avatar of MrError

ASKER

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
Avatar of kretzschmar
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
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?
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.