Solved

Another easy one!

Posted on 2002-03-13
11
660 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

21 Experts available now in Live!

Get 1:1 Help Now