I know you said user can enter any char but at the time of validation
.
However I would like to offer an alternative, because it seems very silly to allow a user to enter values, only to complain to the user 1 minute later; that just smells well... not nice.
I would disallow entry of anything but numbers.
If you have integers thats particularly easy:
Fill in the OnKeyPress event for the editbox.
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char) ;
begin
if not(Key IN ['0'..'9', #8, #9, #13, #27, #127]) then key:= #0;
end;
This will drop anything that's not a number.
If you allow negative numbers you'll need to extra checking to see if the -
has not been entered before.
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char) ;
var
Edit1Text: string;
begin
if (Key = '-') and Pos('-',Edit1.Text) = 0 then begin
Edit1.Text:= '-' + Edit1.Text; //Force the '-' to be in the front.
end
else if (Key = '-') and Pos('-',Edit1.Text) <> 0 then begin //else flip the sign
Edit1Text:= Edit1.Text;
Edit1.Text:= StringReplace(Edit1Text, '-', '',[]);
end;
if not(Key IN ['0'..'9', #8, #9, #13, #27, #127]) then key:= #0;
end;
Because the user can also paste data into an edit box, you'll still have to check the data upon change of the text in the edit.
Because this gets rather fiddly in the ONKeyPress event I use a custom edit component that does this kind of checking and prevents the user from entering foot-in-mouth input into an edit box.
Personally I don't believe in ever issuing an error message, you should always strive to not allow the user to enter invalid data in the first place.