How do I verify that a text box contains only numbers in Delphi?

PawanS picture PawanS · Jun 23, 2011 · Viewed 16.9k times · Source

Might it is very simple question but I never touched delphi. I have a edit box and that can accept character. But on some special condition I have to verify the edit box character are only numbers.

How can we do that?

Note: user can enter any char but at the time of validation I have to verify above one.

Answer

David Heffernan picture David Heffernan · Jun 23, 2011

I don't know what event you intend to use to invoke validation, but the validation can be done like this:

if TryStrToInt(Edit1.Text, Value) then
  DoSomethingWithTheNumber(Value)
else
  HandleNotANumberError(Edit1.Text);