Mega Code Archive

 
Categories / Delphi / Examples
 

How to restrict input in edit boxes

Title: How to restrict input in edit boxes procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); const AllowedChars: string = 'abcdefghijklmnopq' + 'rstuvwxyz01234567_.@'; var i: Integer; Ok: Boolean; begin i := 0; Ok := False; { If you erase next line, user won't be able to type backspace } if Key = #8 then Ok := True; repeat i := i + 1; if Key = AllowedChars[i] then Ok := True; until (Ok) or (i = Length(AllowedChars)); if not Ok then Key := #0; end;