在 Delphi 中如何验证文本框仅包含数字?

发布于 2024-11-16 23:36:19 字数 135 浏览 5 评论 0原文

也许这是一个非常简单的问题,但我从未接触过delphi。 我有一个可以接受字符的编辑框。但在某些特殊情况下,我必须验证编辑框字符只是数字。

我们怎样才能做到这一点?

注意:用户可以输入任何字符,但在验证时我必须验证上述字符。

Might it is very simple question but I never touched delphi.
I have a edit box and that can accept character. But on some special condition I have to verify the edit box character are only numbers.

How can we do that?

Note: user can enter any char but at the time of validation I have to verify above one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

々眼睛长脚气 2024-11-23 23:36:19

我不知道您打算使用什么事件来调用验证,但验证可以这样完成:

if TryStrToInt(Edit1.Text, Value) then
  DoSomethingWithTheNumber(Value)
else
  HandleNotANumberError(Edit1.Text);

I don't know what event you intend to use to invoke validation, but the validation can be done like this:

if TryStrToInt(Edit1.Text, Value) then
  DoSomethingWithTheNumber(Value)
else
  HandleNotANumberError(Edit1.Text);
从此见与不见 2024-11-23 23:36:19

我不明白为什么您希望允许用户输入字符,然后不允许它通过验证。

如果您确实需要阻止进入,那么为您执行此操作的控件比您自己破解更好。如果您的delphi版本确实很旧,那么请尝试一下JVCL组件库中的JVCL:TJvValidateEdit,例如在所有版本的delphi中。然而,在最新的 delphi 版本(2009 及更高版本)中,已经内置了几种可能的解决方案,包括 TMaskEdit 和 TSpinEdit。

如果您确实只需要编写验证方法,请考虑使用正则表达式或手动编码的验证函数,并将该代码与控件分开。

// Taking OP question obsessively literally, this 
// function doesn't allow negative sign, decimals, or anything
// but digits
function IsValidEntry(s:String):Boolean;
var
  n:Integer;
begin
  result := true;
  for n := 1 to Length(s) do begin
    if (s[n] < '0') or (s[n] > '9') then
    begin
       result := false;
       exit;
    end;
  end;
end;

I don't understand why you would want to allow a user to enter a character, and later not allow it to pass validation.

If you really do need to block entry, then a control that does this for you is better than hacking this up yourself. If your version of delphi is really old, then try out the JVCL: TJvValidateEdit in the JVCL component library, for example, in all versions of delphi. However, in regular recent delphi versions (2009 and later), there is already built in several possible solutions including TMaskEdit and TSpinEdit.

If you really only need to write a validation method, then consider using a regex or hand-coded validation function, and keep that code separate from the control.

// Taking OP question obsessively literally, this 
// function doesn't allow negative sign, decimals, or anything
// but digits
function IsValidEntry(s:String):Boolean;
var
  n:Integer;
begin
  result := true;
  for n := 1 to Length(s) do begin
    if (s[n] < '0') or (s[n] > '9') then
    begin
       result := false;
       exit;
    end;
  end;
end;
寄意 2024-11-23 23:36:19

我知道您说过用户可以输入任何字符,但在验证时
不过我想提供一种替代方案,因为允许用户输入值,却在 1 分钟后向用户抱怨似乎很愚蠢;只是闻起来很香……不太好闻。

我不允许输入除数字之外的任何内容。
如果您有整数,那就特别简单:

填写编辑框的 OnKeyPress 事件。

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char) ;
begin
  if not(Key IN ['0'..'9', #8, #9, #13, #27, #127]) then key:= #0;
end;

这将删除所有非数字的内容。

如果允许负数,则需要额外检查 - 之前是否未输入过。

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char) ;
var
  Edit1Text: string;
begin
  if (Key = '-') and Pos('-',Edit1.Text) = 0 then begin
    Edit1.Text:= '-' + Edit1.Text;  //Force the '-' to be in the front.
  end
  else if (Key = '-') and Pos('-',Edit1.Text) <> 0 then begin  //else flip the sign
    Edit1Text:= Edit1.Text;
    Edit1.Text:= StringReplace(Edit1Text, '-', '',[]);
  end;
  if not(Key IN ['0'..'9', #8, #9, #13, #27, #127]) then key:= #0;
end;

由于用户还可以将数据粘贴到编辑框中,因此您仍然需要在编辑中更改文本时检查数据。
因为这在 ONKeyPress 事件中变得相当繁琐,所以我使用一个自定义编辑组件来执行这种检查并防止用户在编辑框中输入脚口输入。

就我个人而言,我不相信发出错误消息,您应该始终努力首先不允许用户输入无效数据。

I know you said user can enter any char but at the time of validation.
However I would like to offer an alternative, because it seems very silly to allow a user to enter values, only to complain to the user 1 minute later; that just smells well... not nice.

I would disallow entry of anything but numbers.
If you have integers thats particularly easy:

Fill in the OnKeyPress event for the editbox.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char) ;
begin
  if not(Key IN ['0'..'9', #8, #9, #13, #27, #127]) then key:= #0;
end;

This will drop anything that's not a number.

If you allow negative numbers you'll need to extra checking to see if the - has not been entered before.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char) ;
var
  Edit1Text: string;
begin
  if (Key = '-') and Pos('-',Edit1.Text) = 0 then begin
    Edit1.Text:= '-' + Edit1.Text;  //Force the '-' to be in the front.
  end
  else if (Key = '-') and Pos('-',Edit1.Text) <> 0 then begin  //else flip the sign
    Edit1Text:= Edit1.Text;
    Edit1.Text:= StringReplace(Edit1Text, '-', '',[]);
  end;
  if not(Key IN ['0'..'9', #8, #9, #13, #27, #127]) then key:= #0;
end;

Because the user can also paste data into an edit box, you'll still have to check the data upon change of the text in the edit.
Because this gets rather fiddly in the ONKeyPress event I use a custom edit component that does this kind of checking and prevents the user from entering foot-in-mouth input into an edit box.

Personally I don't believe in ever issuing an error message, you should always strive to not allow the user to enter invalid data in the first place.

眼藏柔 2024-11-23 23:36:19

但是在一些特殊情况下我有
验证编辑框字符是
只有数字。

您需要两个编辑控件。一种用于仅数字值,另一种用于允许所有值。启用或禁用符合条件的控件。如果这两个控件有良好的标题(也许还有提示,为什么启用或禁用控件),那么用户就知道他必须输入什么以及为什么。

  • 我不喜欢阻止用户。场景:
  • 输入“abc123
  • 我在离开编辑控件时
  • 我收到一条错误消息“仅允许数字”我意识到我忘记了达到特殊条件
  • 我想做一些事情来达到特殊条件条件,
  • 但我不能,因为我总是收到错误消息“仅允许数字”,
  • 所以我必须将值更正为“123”,
  • 执行一些操作以达到特殊条件
  • 重新输入我的旧“abc123”再次值

aarrgghh :-)

对于简单的数据输入表单,我执行以下操作:我允许错误输入,但将每个具有无效输入的编辑控件的字体颜色切换为红色(当空输入时,红色字体颜色不够)值是不允许的)。如果用户尝试发布数据,我会给出一个错误消息,通知用户所有无效的输入字段并中止发布。

But on some special condition I have
to verify the edit box character are
only numbers.

You need two edit-controls. One for the numbers-only value and one for the allow-all value. Enable or disable the control which match the condition. If the two controls have good captions (and perhaps hints, why a control is enabled or disabled) so the user knows what he has to enter and why.

  • I don't like blocking the user. A scenario:
  • I enter "abc123"
  • on leaving the edit control I get an error message "only numbers allowed"
  • I realize that I have forgotten to reach the special condition
  • I want to do something to reach the special condition
  • but I can't because I always get the error message "only numbers allowed"
  • so I have to correct the value to "123"
  • do the things to reach the special condition
  • retype my old "abc123" value again

aarrgghh :-)

For simple data entry forms I do the following: I allow wrong input but switch the font color to red for each edit control with an invalid input (red font color is not enough when an empty value is not allow). If the user try to post the data I give one error message which inform the user about all invalid input fields and abort the post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文