2

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?

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
namco
  • 6,208
  • 20
  • 59
  • 83
  • windows form or webform? for webform Aliostad got the answer for windows overload the onkeypress event an chech the key pressed for values if it's windows form i'll provide the code – Radu Apr 14 '11 at 10:21

3 Answers3

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
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