如何格式化和验证编辑控件只接受浮动或货币值 [英] How is right to format and validate edit control to accept only float or currency values

查看:235
本文介绍了如何格式化和验证编辑控件只接受浮动或货币值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要在编辑控件中接受浮动或货币值。我的问题是我必须做什么来格式化和验证编辑控件输入,所以它只接受数字,逗号或点(逗号或点取决于系统区域设置)。格式是##。##(45.21)。我想要做一个方法,可以控制所有的编辑控制我们浮动格式化和使用验证。

现在我有OnChange事件中使用TryStrToFloat方法的代码,但有时我得到不是浮点数的错误。

也许你们比我做得更多,并且有一些很好的例子来说明如何去做。

如果你想继续使用相同的验证方法,只需增强你的算法来考虑边缘情况(以及你想要如何管理)例如,你可以考虑接受一个空字符串作为一个有效的输入,只是不要抛出异常,或不。您还必须考虑如何在输入格式错误的情况下执行用户交互。例如,如果用户输入了一个无效的数字,则要停止用户在同一毫秒内输入数值...或者您可以采取更自然的方法(例如,验证,直到用户认为一切正确)。

您也可以通过在输入完成时以非正式方式通知用户来进行验证管理,只是对违规字段产生可见效果,如果用户试图保存数据,则可以使用阻止方式(例如使用消息框)。

一个简单的验证函数可能如下所示:

 函数IsEditValidFloat(Sender:TEdit; const AcceptBlank:Boolean = True):Boolean; 
var
sValue:string;
Temp:扩展;
begin
sValue:= Trim(Sender.Text);
if(sValue.Text ='')then
Result:= AcceptBlank
else
结果:= TryStrToFloat(sValue,Temp);
end;

//你可以在OnChangeEvent上调用它:
procedure TForm1.Edit1Change(Sender:TObject);如果IsEditValidFloat(Sender as TEdit),则
开始
,然后
ChangeDisplayState(Sender,dsValid)
else
ChangeDisplayState(Sender,dsError);
end;


I have a application that needs to accept float or currency values in edit control. My question is what I must to do to format and validate edit controls input so it accepts only numbers, comma or dot (comma or dot depends on system locale). And the formatting is ##.## (45.21). I want to do one method that can control all edit controls wehre float formatting and validating is used.

Right now I have code in OnChange event that uses TryStrToFloat method, but sometimes I get "'' is not floating point number" errors.

Maybe you guys have done it more than me and have some great examples how to do it right.

解决方案

If you want to continue using the same validation approach, just enhance your algorithm to consider the edge cases (and how you want to manage that).

For example, you can consider accepting an empty string as a valid input and just don't throw an exception, or not. You must also consider how do you want to perform the user interaction in case of malformed input. For example if a user enters a invalid number, you want to stop the user to input values at that same millisecond... or you can take a more natural approach (for example, validating until the user thinks everything is correct).

You can also manage validation just by notifying the user in a non-stoper way while the input is being done, just making a visible effect over the offending fields, and in a stopper way (for example with a message box) if the user tries to save the data.

A simple validation function may look like this:

function IsEditValidFloat(Sender: TEdit; const AcceptBlank: Boolean = True): Boolean;
var
  sValue: string;
  Temp: Extended;
begin
  sValue := Trim(Sender.Text);
  if (sValue.Text = '') then
    Result := AcceptBlank
  else
    Result := TryStrToFloat(sValue, Temp);
end;

//you might call this on the OnChangeEvent:
procedure TForm1.Edit1Change(Sender: TObject);
begin
  if IsEditValidFloat(Sender as TEdit) then
    ChangeDisplayState(Sender, dsValid)
  else
    ChangeDisplayState(Sender, dsError);
end;

这篇关于如何格式化和验证编辑控件只接受浮动或货币值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆