[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

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
0
MrError
Asked:
MrError
1 Solution
 
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
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now