请问如何判断在edit中输入的是数字还是字母?

解决方案 »

  1.   

    在onkeypress中判断key 是不是‘0’-‘9’或者字母
      

  2.   

    try
      aa:=strtoint(edit.text);// strtofloat(edit.text)
      //数字
    except
      //非数字
    end;
      

  3.   

    在KeyPress(Sender: TObject; var Key: Char);
    begin
      if Not (Key In ['0'..'9','.',#8]) then
         Key := #0;
    end;
    #8是退格键,#0是空
      

  4.   

    同意 charles2118(第六元素) ( 
    在KeyPress(Sender: TObject; var Key: Char);
    begin
      if Not (Key In ['0'..'9','.',#8]) then
         Key := #0;
    end;
    #8是退格键,#0是空
      

  5.   

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

  6.   

    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;