开发者

How do I verify that a text box contains only numbers in Delphi?

开发者 https://www.devze.com 2023-03-15 09:32 出处:网络
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

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.


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);


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;


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消