Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread Integers in TEdits.
Tue, Jan 24 2012 3:05 AMPermanent Link

Steve Gill

Avatar

Hi Tim,

I've been trying to work out how to validate the TEdit text property to make sure only integers are entered.  I found out something interesting (that works):

var
  UserAge: integer;

UserAge := StrToInt(edtAge.Text);
if IntToStr(UserAge) = 'NaN' then
begin
    // Not a valid number
end;

Does NaN stand for "Not a Number"?  Is this a bug or is this the correct way to check for numbers?

Steve
Tue, Jan 24 2012 4:58 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Steve


Does that work in Delphi as well? If so which version?

Normally I've used a try..except block to see if a number was a number. However, I'd generally use the OnKeyPress event to stop anything other than 0..9 being entered.

Roy Lambert
Tue, Jan 24 2012 5:11 AMPermanent Link

Walter Matte

Tactical Business Corporation


It is in Delphi too....

http://www.delphibasics.co.uk/RTL.asp?Name=NaN


Walter
Tue, Jan 24 2012 8:01 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Steve,

<< Does NaN stand for "Not a Number"?  Is this a bug or is this the correct
way to check for numbers? >>

This is one of those areas where native Delphi's OP and EWB's OP deviate.
If a string isn't a valid number, then you will not receive an exception,
rather the result will be NaN.  This also applies to floating-point numbers
(Double).

--
Tim Young
Elevate Software
www.elevatesoft.com
Tue, Jan 24 2012 8:06 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Walter


That's totally unrelated to Steve's post. It uses a specialised function (IsNaN) testing numbers. Steve has a StrToInt(string) = 'NaN' but AFAIK in Delphi (up to D2006) doesn't have an overridden version of StrToInt which would produce a string result.

Roy Lambert
Tue, Jan 24 2012 8:50 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Tim

>This is one of those areas where native Delphi's OP and EWB's OP deviate.
>If a string isn't a valid number, then you will not receive an exception,
>rather the result will be NaN. This also applies to floating-point numbers
>(Double).

That's what I wanted to know. Nice booby trap Smiley

Roy Lambert
Tue, Jan 24 2012 4:03 PMPermanent Link

Steve Gill

Avatar

Hi Tim,

<< This is one of those areas where native Delphi's OP and EWB's OP deviate.
If a string isn't a valid number, then you will not receive an exception,
rather the result will be NaN.  This also applies to floating-point numbers
(Double).  >>

Thanks, makes sense.  Yeah, I tried a try..except block first and found it didn't work.  Then I put a couple of ShowMessage's in to see what was coming back when I entered a non-number value.

Steve
Image