We help IT Professionals succeed at work.

Checking if input is a numeric value

mario_34
mario_34 asked
on
Medium Priority
337 Views
Last Modified: 2010-04-06
Hi,

how can i check if a value in a text box is a numeric value?

Thanx
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
just from head

function is_numeric(value :string):Boolean;
var
  testvalue : Value;
  err : Integer;
begin
  val(value,testvalue,err);
  result := err = 0;
end;

meikl ;-)  
CERTIFIED EXPERT
Top Expert 2004

Commented:
or just from the dephi online help about val
example

var

  I, Code: Integer;
begin
  { Get text from TEdit control }
  Val(Edit1.Text, I, Code);
  { Error during conversion to integer? }
  if Code <> 0 then
    MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0);
  else
    Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));

end;

;-)

Commented:
Here is another way

try
  StrToInt(EditBox.Text);
except
  ShowMessage(EditBox.Text + ' is not valid a mumber');
end;


The Crazy One

Author

Commented:
Hi kretzschmar,

when i try to run the code (i used the code from the second comment) i get the error message: "Constant expression expected." What's wrong?

Commented:
Code to only allow numeric keys as input

procedure TForm.EditBoxKeyPress(Sender: TObject;
  var Key: Char);
begin

     if not (Key in [#8, #48..#57]) then Key := #0;

end;


#8 is the Backspace if you want to allow for decimal then include #46 and if you want to allow negative numbers inclued #45

Commented:
the following code on the keypress event will only allow
1-9 or . or backspace to be entered into a editbox:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key <> #8 then begin
  if not(key in ['0'..'9','.'])then key := #0;
 end;
end;

Commented:
sorry crazybloke was checking first to see i had right key event and not keydown/up event which i usually write by mistake ;-)

Commented:
:>)

Author

Commented:
Hi,

why should i check that only numeric keys are pressed? Isn't the val function for that purpose, or is this the reason for the error message? Anyway, could anybody explain me what does that message mean. I get the same message when trying to make a substraction:

var a,b:real;

a:=90-b;
CERTIFIED EXPERT
Top Expert 2004
Commented:
mario,

couldn't reproduce your problem

tested with d3 and this sample unit
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var

 I, Code: Integer;
begin
 { Get text from TEdit control }
 Val(Edit1.Text, I, Code);
 { Error during conversion to integer? }
 if Code <> 0 then
   MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0)
 else
   Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
end;

end.

meikl ;-)

Author

Commented:
Hi kretzschmar,

i also have delphi 3...maybe it's something wrong with the settings of the compiler??

CERTIFIED EXPERT
Top Expert 2004

Commented:
? maybe,
but i've also played with this with no same result as you

CERTIFIED EXPERT
Top Expert 2004

Commented:
? maybe its the real-type, use double-type instead

Explore More ContentExplore courses, solutions, and other research materials related to this topic.