开发者

RegularExpression - Positive Integer with 1 Decimal Point

开发者 https://www.devze.com 2023-04-07 14:28 出处:网络
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 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

精彩评论

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

关注公众号