如何判断输入的是数字?!

bengan 2003-08-12 02:05:37
请问如何判断在edit中输入的是数字还是字母?
...全文
95 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pressman 2003-08-13
  • 打赏
  • 举报
回复
同意 charles2118(第六元素) (
在KeyPress(Sender: TObject; var Key: Char);
begin
if Not (Key In ['0'..'9','.',#8]) then
Key := #0;
end;
#8是退格键,#0是空
xiufengy 2003-08-13
  • 打赏
  • 举报
回复
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;
charles2118 2003-08-13
  • 打赏
  • 举报
回复
多谢songandlan(小松一夜听春雨)

那加一个判断吧
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;

我想输入时第一个字符也不能是‘.’的吧,大家再想想还有什么漏洞没?
我上面是随便写写的,有什么好的算法,或者是写法,大家一起讨论!


songandlan 2003-08-13
  • 打赏
  • 举报
回复
不完全同意.
還沒有控制小數點只能有一個的問題
如樓上所說
那麼可是可以輸入如10.11.456的無效數字
所以還要先判斷一下是否已有小數點,
errorkiss 2003-08-12
  • 打赏
  • 举报
回复
是的 ,,,,up
Vevel 2003-08-12
  • 打赏
  • 举报
回复
pluto果然有一套
charles2118 2003-08-12
  • 打赏
  • 举报
回复
在KeyPress(Sender: TObject; var Key: Char);
begin
if Not (Key In ['0'..'9','.',#8]) then
Key := #0;
end;
#8是退格键,#0是空
pluto74 2003-08-12
  • 打赏
  • 举报
回复
try
aa:=strtoint(edit.text);// strtofloat(edit.text)
//数字
except
//非数字
end;
李_军 2003-08-12
  • 打赏
  • 举报
回复
在onkeypress中判断key 是不是‘0’-‘9’或者字母

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧