Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Another easy one!

Posted on 2002-03-13
11
Medium Priority
?
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

670 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