1.只能输入数据
if (Key<'0') and (Key>'9') and (Key<>'-') and (Key<>'.') then Key:= #0;
if( (Key='-') and ( (Length(TEdit(Sender).Text)>0) ))then Key:=#0;
if((Key='.') and ( (Length(TEdit(Sender).Text)>0) and ( Pos(TEdit(Sender).Text,'.')>0) )) then Key:=#0;
2.禁止输入全角字符
If (Key=#163) then
Begin
Key:=#0;
showmessage('不能输入全角字符,将输入法改为半角');
end;
If (Key=#161) then
Begin
Key:=#0;
end;
If (Key=#170) then
Begin
Key:=#0;
end;
3.函数实现
Function PLstrtoInt(ss:string):Int64;
var
plst,pls,plss,plsss:string;
NOdian,NOjianhao,NOjiahao:boolean;
plf:Int64;
i:integer;
begin
plss:='';
NOdian:=true;
NOjianhao:=true;
NOjiahao:=true;
pls:=trim(ss);
for i:=1 to length(pls) do
begin
plst:=copy(pls,i,1);
if (plst='0') or (plst='1') or (plst='2') or
(plst='3') or (plst='4') or (plst='5') or
(plst='6') or (plst='7') or (plst='8') or
(plst='9') or
((plst='.') and Nodian) or
((plst='+') and Nojiahao) or
((plst='-') and Nojianhao)
then plss:=plss+plst;
if (plst='.') then Nodian :=false;
if (plst='+') then Nojiahao :=false;
if (plst='-') then Nojianhao :=false;
end;
Plsss:=copy(plss,1,1);
plst:='';
if (length(plss)>=2) then
begin
for i:=2 to length(plss) do
Begin
plst:=copy(plss,i,1);
if (plst<>'+') and (plst<>'-') then plsss:=plsss+plst;
End;
plf:=strtoInt(plsss);
end
else
begin
if (plss='-') Or (plss='+') Or (plss='') then plf:=0
else plf:=strtoInt(plss);
end;
result:=plf;
end;
4.生成可以为SQL用的语句
Function PLstrtoNotFuHao(ss:string):String;
var
pls:string;
begin
pls:=trim(ss);
while Pos(' ', plS) > 0 do
plS[Pos(' ', plS)] := '0';
while Pos('''', plS) > 0 do
plS[Pos('''', plS)] := '0';
while Pos(':', plS) > 0 do
plS[Pos(':', plS)] := '0';
while Pos(';', plS) > 0 do
plS[Pos(';', plS)] := '0';
while Pos('"', plS) > 0 do
plS[Pos('"', plS)] := '0';
result:=pls;
end;
那加一个判断吧
function checkTextContainsPonitOrIsEmpty(str:string):boolean;
begin
if str=''then
begin
result:=true;
exit;
end;
if pos('.',str)>0 then
result:=True;
else
result:=false;
end;
KeyPress(Sender: TObject; var Key: Char);
begin
if checkTextContainsPonitOrIsEmpty(eidt1.text) then
begin
if Not (Key In ['0'..'9',#8]) then
Key := #0;
end
else
if Not (Key In ['0'..'9','.',#8]) then
Key := #0;
end;