By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,496 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,496 IT Pros & Developers. It's quick & easy.

Check value is number only

P: n/a
I want to check the text in a text box once the user tabs out of textbox, for
number only.

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Mike L" <Ca***@nospam.nospam> wrote in message
news:28**********************************@microsof t.com...
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
Nov 17 '05 #2

P: n/a
KH
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.
"Mike L" wrote:
I want to check the text in a text box once the user tabs out of textbox, for
number only.

Nov 17 '05 #3

P: n/a
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

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

Nov 17 '05 #4

P: n/a
Brendan Grant <gr****@NOSPAMdahat.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.
Nov 17 '05 #6

P: n/a
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
"Jon Skeet [C# MVP]" wrote:
Brendan Grant <gr****@NOSPAMdahat.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7

P: n/a
Brendan Grant <gr****@NOSPAMdahat.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.