Solved

Another easy one!

Posted on 2002-03-13
11
657 Views
Last Modified: 2012-08-13
This is already my 3rd easy Q regarding Delphi in the past 3 days.  For those who didn't get to see the first two, know that I haven't done any Delphi in a long time so please be easy on the laughing!

Is there some kind of equivalent function of the VB6 IsNumeric in Delphi ?  For those who aren't familiar with the VB function, know that it returns a boolean value depending on if the variable is numeric or not.

--
'VB example:
dim myVar as integer
myVar = 10
if IsNumeric(myVar) then doSomeThing
--

I just want to validate an entry from an editbox.  I don't want to try searches in a database if the entry isn't numeric.

Any hint ??
Thanks!
0
Comment
Question by:qas
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 6861931
there are more possibilites

first

  try
     intvar := IntToStr(edit1.text);
  except
     showmessage('No Integer');
  end;

second
  use the val-procedure->see delphi online help

maybe more possibilities,

keep in mind, that you can expand [object] pascal
with own functions

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6861946
sample

function isnumeric(s : String) : boolean;
var
   dummyInt : Integer;
begin
  result := true;
  try
   dummyInt := StrToInt(s);
  except
    result := false;
  end;
end;

meikl ;-)      
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 6861950

Another possibility...

function IsNumeric(const Value: Variant): Boolean;
begin

  try
     VarAsType(Value, varDouble);
     result:=True;
  except
     result:=False;
  end;

end;

Would allow you to pass pretty much anything
ie:

  IsNumeric(3.455);
  IsNumeric(12);
  IsNumeric(Edit1.Text);
  IsNumeric('23');


If you only want to validate for integers, change the VarAsType to

VarAsType(Value, varInteger);

-------
Russell
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6862399
VarAsType is not going to work. Example:

function IsNumeric(const Value: Variant): Boolean;
begin
 try
    VarAsType(Value, varString);
    result:=True;
 except
    result:=False;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var num:smallint;
begin
 num:=8;
 if isnumeric(num) then showmessage('True');
end;



Shows "True".


StrToInt is a safer bet, although you could do something like this also:

function IsNumeric(const Value: String): Boolean;
var Num:set of char;
    Loopint:integer;
begin
    Num:=['0'..'9'];
    For loopint:=1 to Length(value) do
    begin
      if (not (Value[loopint] in Num)) then
      begin
         Result:=False; /// no use going on
         exit;// so get out.
      end;
     Result:=True;/// got this far, so it's good.
    end;
end;



Good luck!!


0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6862413
The advantage of my code versus StrToInt is that it won't raise an exception, even in the IDE. It is also more partable in terms of integer types. StrToInt is limited to I believe cardinal before you have to use StrToInt64.


Good luck!!



0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Expert Comment

by:Russell Libby
ID: 6862519

DrDelphi,

I'm not sure I understand why VarAsType is not going to work. I did not use "VarAsType(Value, varString)" as you did, but "VarAsType(Value, varDouble)", which falls under the variant rules of conversion as follows:

--------------
integer->real
real->real
string->real (using windows regional settings)
char(same as string)->real
boolean->real (0, -1)
unassigned->real (0)
--------------

If the value cannot be changed to a double value, then an exception is raised and false is returned.




0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6863229
Edit box is just a string so..
Returns true for numeric strings such as 4, 7.03 and 2.2E-15

function IsNumeric(S: String): Boolean;
begin
  try
    StrToFloat(S);
    Result := True;
  except
    Result := False;
  end;
end;

Regards,
Steve
0
 

Expert Comment

by:Rabster
ID: 6864528
I always use the val procedure as mentioned by kretzschmar as a second option. It's easy and does not generate errors.

Var
  I,Code : Integer;
begin
  Val(Edit1.Text,I,Code);
  If Code = 0 then
  ShowMessage(Edit1.Text + ' is numerical')
  else
  ShowMessage(Edit1.Text + ' is not numerical,             character '+ IntToStr(Code) + ' is not numerical.');
end;
0
 

Author Comment

by:qas
ID: 6864691
The val stuff worked perfectly!  
I liked the idea to not raise exceptions.

Thanks for all the answers!
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6864926
Oh dear.. The idea IS to raise exceptions, doh!!
Hence the exceptions raised by standard functions like StrToInt(). (and StrToFloat)
Therefor you just joined the bicycle race on a pogo stick, oops.
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6866285
Actually, reading that back sounds terrible, sorry everyone. I don't mean you personally gas. That's the last bit of toungue-in-cheek I type. Agreed Val() is perfect when you need the position in the string as well.

It's just that there's nothing to worry about with exceptions. It's easy to make the mistake of coding around them when new to Delphi. They should be used accordingly rather than ignored.

Regards,
Steve


0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now