For Newbie

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;
begin
  result:=false;
  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;
    end;
  end;
end;


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');
  
exit;
 
end;
 
ShowMessage ('Data is stored'); end;
In the example above, the parameter "self" is used for validation on the form itself.
 

Share on Facebook
Share on Twitter
Share on Google+

Related : Quick Input Validation in Delphi