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.
加载中,请稍侯......
精彩评论