Reputation: 6338
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?
Upvotes: 2
Views: 15042
Reputation: 81660
Use this expression
string expression = @"^[1-9]\d*$";
For anyone wanting to test the expression, use this link: http://www.rubular.com/r/1JIPP8E1zH
Upvotes: 12
Reputation: 3385
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.
Upvotes: 1
Reputation: 152216
Try with:
/^[1-9][0-9]*$/
What length of string you accept ?
If you allow empty string too try with:
/^([1-9][0-9]*)?$/
Upvotes: 1