procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin // #8 is Backspace if not (Key in [#8, '0'..'9']) then begin ShowMessage('Invalid key'); // Discard the key Key := #0; end; end;