Check value is number only

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to check the text in a text box once the user tabs out of textbox, for
number only.
 
Mike L said:
I want to check the text in a text box once the user tabs out of textbox,
for
number only.

Why not trap the KeyPress event and allow numbers only?

private void OnKeyPress(KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar)
{
// Swallow this invalid key
e.Handled = true;
}
}

HTH,

Mike Rodriguez
 
You could use the Parse method of the type of number you want (short,
decimal, int, etc.) and wrap it in a try/catch -- if an exception is thrown
it's not a number.

Generally throwing exceptions to do control-of-flow logic isn't the right
approach, but there's no functions to indicate whether a string can be
converted to a number, so this may be a decent approach if performance isn't
critical for your app.

In .net 2.0 there are TryParse() methods that return bool, so you would ba
able to do this without the try/catch.
 
Try these two functions. The first tells you if the string passed in is a
number (int or float), while the second just tells you if it is an integer.

public static bool IsNumber(String strNumber)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern =
"^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern +")|("
+ strValidIntegerPattern + ")");

return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}

public static bool IsInteger(String strNumber)
{
Regex objNotNaturalPattern=new Regex("[^0-9]");
Regex objNaturalPattern=new Regex("[0-9][0-9]*");

return !objNotNaturalPattern.IsMatch(strNumber) &&
objNaturalPattern.IsMatch(strNumber);
}

Brendan
 
Brendan Grant said:
Try these two functions. The first tells you if the string passed in is a
number (int or float), while the second just tells you if it is an integer.

I find that complicated regular expressions, when unnecessary,
seriously detract from readability and could easily lead to subtle
bugs.

They also don't perform as well as "brute force" hard coded tests in
simple cases.

Using a hard-coded test followed by int.Parse (or whatever parse) is
more readable, simpler, and more efficient than using regular
expressions.

(The difference in terms of performance is quite impressive, although
of course it'll only be significant in extreme cases where you're
parsing millions of the things.)

Regexes are very powerful, when used in the right place - but I believe
in this case it's like boarding an aeroplane in order to travel a
hundred metres.
 
Regexes are very powerful, when used in the right place - but I believe
in this case it's like boarding an aeroplane in order to travel a
hundred metres.

Well said - I couldn't agree more.
 
Code readability is always good, however there are times when performance is
more important than readability, and with extensive testing one can
dramatically reduce the risk of such a complicated regular expression from
causing you issues.

While using a Parse() may be simpler and more readable, I disagree that it
is more efficient. Usage of either depends on which side you want to pay the
penalty of an unparseable value.

If it is a parseable value being passed in, then yes, Parse() faster, an
order of magnitude faster in the best case. On the other hand, if a lot of
invalid values are being dealt with, using the Regex method can be in the
best case ~50 times faster than Parse().

Brendan
 
Brendan Grant said:
Code readability is always good, however there are times when performance is
more important than readability, and with extensive testing one can
dramatically reduce the risk of such a complicated regular expression from
causing you issues.

While using a Parse() may be simpler and more readable, I disagree that it
is more efficient.

I don't think you read my post properly. I wasn't suggesting *just*
using int.Parse - I was suggesting a simple method to test for just
digits (without using a regular expression) followed by a call to
Parse.
Usage of either depends on which side you want to pay the
penalty of an unparseable value.

If it is a parseable value being passed in, then yes, Parse() faster, an
order of magnitude faster in the best case. On the other hand, if a lot of
invalid values are being dealt with, using the Regex method can be in the
best case ~50 times faster than Parse().

Whereas a hard-coded test followed by int.Parse can be about 50x faster
than Regex.
 
Back
Top