Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsDigit function 1

Status
Not open for further replies.

michaenh

Programmer
Aug 7, 2002
205
NO
Hi everyone.

Is there any similar function in Delphi? ISDIGIT?
This function is to check if the value is a number(digit).
I have used this in Java and C++.

In delphi I want to check the user input from a combobox. If the user write some text then a message box will prompt that the user only can write numbers.

Maybe there are some easy properties?

Million thanks
Michael
 
I use something like:

For i := 1 to Length(edtQty.Text) do //Check it's a number
If (edtQty.Text[ i ] < '0') or (edtQty.Text[ i ] > '9') then Accept := False;

Ignoring the spaces within the [ and ] obviously.
 
You could also test StrToInt (or StrToFloat) inside a try block

Try
StrToInt(Edit1.text);
except
Edit1.Field.Clear;
Showmessage('Use a number');
end;

if the editbox is not data aware you would use something like

Try
i := StrToInt(Edit1.text);
except
Edit1.text := '';
Showmessage('Use a number');
end;
Brian
&quot;There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group.&quot; - tag line I stole
 
Instead of slapping the user's hand when he makes a mistake, I prefer to prevent the mistake from being made in the first place when possible.

For example, here's a way to restrict the user when only numbers are valid in a text field:
Code:
{User typing an account number.  Restrict entry to numbers, Esc and Ctrl-Z}
Code:
procedure TQSpecs.dfAccountKeyPress(Sender: TObject; var Key: Char);
begin
  if Key in [#26,#27] then dfAccount.Text := IntToStr(FAccountKey);
  if not (Key in ['0'..'9',#8,#26,#27]) then Key := Char(0);
end;
 
...I just have the following in mine:

procedure TFormCoList.edCoIDKeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9', #8,#9]) then key := #0;
end;
 
I allow the users to use Esc (#27) and Ctrl-Z (#26) as an &quot;undo&quot; by restoring the original value that was captured on entry in a TEdit. (Part of my definition of &quot;user friendly.&quot;) -- Use of the Escape key is old school. Ctrl-Z seems to be the new Windows standard. But, probably not needed for a list/combo box.

Tab (#9) is used by Windows and is not sent to the procedure, so no need to look for it.
 
Wow! Thank you guys.. :eek:)

Everyone should get a star here, but it would be to much stars in this thread. So I will give two stars to Zathras. I like the way you are thinking.

Thanks guys.

Michael
 
On a different tack, have a look at the MaskEdit component, you can set this up to allow only numeric input.
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top