Connecting Tech Pros Worldwide Help | Site Map

Check value is number only

Mike L
Guest
 
Posts: n/a
#1: Nov 17 '05
I want to check the text in a text box once the user tabs out of textbox, for
number only.

Michael Rodriguez
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Check value is number only


"Mike L" <Cadel@nospam.nospam> wrote in message
news:2863D3FC-D60A-4790-A310-918F71A66D34@microsoft.com...[color=blue]
>I want to check the text in a text box once the user tabs out of textbox,
>for
> number only.
>[/color]

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


KH
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Check value is number only


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:
[color=blue]
> I want to check the text in a text box once the user tabs out of textbox, for
> number only.
>[/color]
Brendan Grant
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Check value is number only


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:
[color=blue]
> I want to check the text in a text box once the user tabs out of textbox, for
> number only.
>[/color]
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Check value is number only


Brendan Grant <grantb@NOSPAMdahat.com> wrote:[color=blue]
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Mark Rae
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Check value is number only


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1d40ea57b4b58fc698c4a1@msnews.microsoft.c om...
[color=blue]
> 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.[/color]

Well said - I couldn't agree more.


Brendan Grant
Guest
 
Posts: n/a
#7: Nov 17 '05

re: Check value is number only


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:
[color=blue]
> Brendan Grant <grantb@NOSPAMdahat.com> wrote:[color=green]
> > 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.[/color]
>
> 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 - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>[/color]
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#8: Nov 17 '05

re: Check value is number only


Brendan Grant <grantb@NOSPAMdahat.com> wrote:[color=blue]
> 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.[/color]

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.
[color=blue]
> 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().[/color]

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

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