Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Validate numeric textbox using int.tryparse visual C#.NET

5.00/5 (2 votes)
15 Sep 2011CPOL 9.9K  
The alternative posted by Daaron does not work for several cases. E.g. Sign is included (+ or -), text contains whitespace prefix or suffix, etc. You could fix the first two limitations with a more detailed regex like (^s*(-|+)?d+s*$) but you could still end up with a value that won't fit...

The alternative posted by Daaron does not work for several cases. E.g. Sign is included (+ or -), text contains whitespace prefix or suffix, etc. You could fix the first two limitations with a more detailed regex like "(^\s*(-|\+)?\d+\s*$)" but you could still end up with a value that won't fit in type 'int'. IMHO, int.TryParse is the best way to validate.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)