I have a textbox on the form and I want the user to be able to enter only numbers, and first number can not be zero. Which pattern must be in this case?
Asked
Active
Viewed 1.5k times
3 Answers
12
Use this expression
string expression = @"^[1-9]\d*$";
For anyone wanting to test the expression, use this link: http://www.rubular.com/r/1JIPP8E1zH

Aliostad
- 80,612
- 21
- 160
- 208
-
-
@Namco, why?? Of course it does... Have a look here http://www.rubular.com/r/1JIPP8E1zH – Aliostad Apr 14 '11 at 10:24
-
1
Try with:
/^[1-9][0-9]*$/
What length of string you accept ?
If you allow empty string too try with:
/^([1-9][0-9]*)?$/

hsz
- 148,279
- 62
- 259
- 315
1
Alternatively, and I would suggest this for localization purposes, consider
double.Parse(myTextBox.Text, System.Globalization.CultureInfo.CurrentCulture);
as alternative. It parses numbers with decimals or whatever variation you like according to the settings (culture) which is installed. In my country, with a dot as decimals seperator.

Jaapjan
- 3,365
- 21
- 25
-
Nice alternative, just to note `TryParse` might be more useful, depending. – Grant Thomas Apr 14 '11 at 10:35