onKeyPress Event
if not (key in ['0'..'9', #8, #13]) then
begin
key := #0;
end;
If not (key in [#65..#90,#48..#57,'0'..'9', #8, #13]) then //#97到#122就是小寫字母a到z的ASCII碼,#65到#90是大寫,#48到#57是數字0到9的ASCII碼。如果按下的key不在這些範圍內,下面:
begin
key:= #0; //忽略掉用戶按的鍵,別顯示出來
ShowMessage('只能輸入大寫字母或數字');//顯示對話框
// end else //否則,在以上範圍內的話
// if (Edit1.SelStart=0) and (not (key in [#97..#122,#65..#90])) then //判斷,如果光標在第一位置,並且key不在大小寫和數字範圍內,下面:
// begin
// Key:=#0; //忽略掉用戶按的鍵,別顯示出來
// ShowMessage('必須以字母開頭'); //並顯示對話框
end;
uses Dialogs;
var
I, Code: Integer;
begin
{ Get text from TEdit control }
//Numeric Only checking Val()
Val(Edit1.Text, I, Code);
{ Error during conversion to integer? }
if Code <> 0 then
MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0);
else
Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
end;
//invalid characters
procedure GetValueTrat(const aValue: string);
const
CHARS = ['0'..'9', 'a'..'z', 'A'..'Z'];
var
i: Integer;
begin
for i := 1 to Length(aValue) do
begin
if not (aValue[i] in CHARS) then
raise Exception.Create('Please fill out correct characters in [''0''..''9'', ''a''..''z'', ''A''..''Z'']');
end;
end;