开发者

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 num开发者_如何学Pythonbers, and first number can not be zero. Which pattern must be in this case?


Use this expression

string expression = @"^[1-9]\d*$";

For anyone wanting to test the expression, use this link: http://www.rubular.com/r/1JIPP8E1zH


Try with:

/^[1-9][0-9]*$/

What length of string you accept ?

If you allow empty string too try with:

/^([1-9][0-9]*)?$/


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜