namco
namco

Reputation: 6338

Regular expression to match numbers only not starting with zero

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

Answers (3)

Aliostad
Aliostad

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

Jaapjan
Jaapjan

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

hsz
hsz

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

Related Questions