开发者

RegularExpression - Positive Integer with 1 Decimal Point

I can't seem to get the syntax correct for a 开发者_JS百科RegularExpression using C# to only allow positive numbers with up to 1 decimal point.

I have the following DataAnnotation for positive integers working:

[RegularExpression(@"[^\-][\d\.]*", ErrorMessage = "Positive integers only")]  

Any tips?


You want ^\d+(\.\d)?$.


[RegularExpression(@"^\d+(\.\d)?$", ErrorMessage = "Positive integers only")]


I propose ^(0|[1-9]\d*(\.\d)?)$. That way you also rule out things like 0001.


You may try @"^\d+([.]\d?)?$"
The "." is a special character and has to be escaped, otherwise the answer by SLaks is alright.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜