Demonstrates how to validate of input real number in TEdit box in Delphi
Demonstrates how to validate of input real number in TEdit box in Delphi.
Bookmark:
Demonstrates how to validate of input real number in TEdit box in Delphi
Check validation of input real number in TEdit box and format it into two decimal places.
In order to check the validation, its better to write procedure. So you can call this procedure for all the Edit components within your project.
procedure CheckReal(Var EdBox : TEdit); Var R : Real; Code : Integer; Begin { Get text from TEdit control } Val(EdBox.Text, R, Code); { Val - Converts a string to a numeric representation. } { Error during conversion to Real? } If Code<>0 Then Begin R:=0; MessageDlg('Please check your input details ?', mtError, [mbOk], 0); EdBox.Text := '0.00'; { Get focus back to edit box } EdBox.SetFocus; End Else { Pass formatted (with two decimal places) real value to text box } EdBox.Text := Format('%f', [R]); { Format - Returns a formatted string assembled from a format string and an array of arguments. } End;
Now you can call above procedure in Edit component OnExit event.
procedure TForm1.Edit1Exit(Sender: TObject); begin CheckReal(Edit1); end;
Download This Delphi Tutorials.
Download materials for this article (Delphi - Tutorials)
Check-validation-for-input-real.zip
File size: 7 KB, File type: zip
Total downloads: 405, Upload date: February 02 - 2009