65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Sep 12, 2011

CPOL
viewsIcon

9967

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.