Delphi Tips Trick

For Newbie

Iklan Responsive Atas

Responsive Ads Here

Friday, 18 November 2011

Quick Input Validation in Delphi

Why do I have to say how fast, because this way we no longer need to type the script validation for each field. For more details, for example, we created a form with four Edit and a Button component therein. The fourth example of the field there are three fields are required. Change the property "tag" from the Edit these three components into one, and the others leave it 0. It aims to tell which fields are mandatory and which are not. After that type in the script below. This function will be used for input validation when submit button is clicked.

function inputBlank(frm:TForm):boolean;
var i: integer;
  with frm do begin
    for i:=0 to ComponentCount-1 do begin
      if (Components[i] is TEdit)and(Components[i].Tag = 1) then
        if TEdit(Components[i]).Text = '' then result:=true;

Notice in the above functions are parameter "frm" of type "TForm". Fill in the name of the form where the fields will be validation for this parameter. For example, add the following script on the event "onClick" your submit button.

procedure TForm1.Button1Click (Sender: TObject); begin
if inputblank (self) then begin
ShowMessage ('There's still an empty field');
ShowMessage ('Data is stored'); end;
In the example above, the parameter "self" is used for validation on the form itself.

No comments: