It is better to put the permission keys in the set as a constant (speed, optimization):
Updated # 2 Allow only one decimal char and handle DecimalSeparator correctly.
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
const
Backspace =
AllowKeys: set of Char = ['0'..'9', Backspace];
begin
if Key = '.' then Key := DecimalSeparator;
if not ((Key in AllowKeys) or
(Key = DecimalSeparator) and (Pos(Key, Edit1.Text) = 0)) then
begin
ShowMessage('Invalid key: ' + Key);
Key :=
end;
end;
For more accurate results, take a look at the TNumericEdit components included with DevExpress, JVCL, EhLib, RxLib, and many other libraries.