我在SQL理设置的字段是char类型,所以有的时候操作员录入数据时手动错误,22.5录成了22..5,我希望回车的时候能提示出错,让操作员重新录入,如何写?
或者有时操作员只写了个小数点.进去,这样的时候也提示错误,反正只要不是正确数字、小数这种的,都提示错误。
因为在最后我要对这一行字段的数据进行计算平均值,当有这种不符合的数据时,计算平均值时就会出错误。

解决方案 »

  1.   


    判断str是否符合double类型
    function IsNumber(str: string): boolean;
    var
      code:integer;
      num:double;
    begin
      Val(str, num, code);
      if Trim(str) = '' then
        str := '0'
      else if code > 0 then
      begin
        Result := False;
        if Trim(str) = '' then
          str := '0'
        else
          str := LeftStr(str,code-1);
      end;
      Result := code = 0;
    end;
      

  2.   


    Edit1.OnKeyPress:var
      TempValue: Double;
    begin
      if Key = #13 then
        try
          TempValue := StrToFloat(Edit1.Text);
        except
          ShowMessage('输入数据格式非法!');
          Edit1.Clear;
          Edit1.SetFocus;
        end;
    end;
      

  3.   

    var
      TempValue: Double;
    begin
      if (Key = #13) and  not tryStrToFloat(Edit1.Text,TempValue) then
      begin
          ShowMessage('输入数据格式非法!');
          Edit1.Clear;
          Edit1.SetFocus;
      end;
    end;
      

  4.   

    或者用个支持mask的控件,限制输入格式
      

  5.   

    function CheckFloatValid(const inputStr:string):boolean;
    begin
      try
        strToFloat(inputStr);
      except
        on EconvertError do
         begin
           result := False ;
           exit;
         end;
      end;
      Result := True;
    end;
      

  6.   

    谢谢大伙了!bdmh和corn1的代码,我用的时候,都是先弹出个错误,也许是还有什么其他地方要设置的我不会设置。byteh的一下子就出来了!
    zxf_feng的我没有试过。