1

I'm continuing to cleanup a mess of a site at my workplace and am almost done adding validation, I have this one issue remaining.

I have a number that is set by default to 100.0. I need to allow input from 0.1 to 100.0. I currently have an expression that works but allows 0 and 0.0 and 00.0, and these values crash my program. Here is my current expression:

^(\d{0,2}(\.\d{1})?|100(\.0?)?)$

The current expression allows numbers such as 0.2 .2 and 00.2, that is fine, I just can't have it accept 00.0, 0.0 and 0.

Again, this is numbers only, one or no decimals, and 0.1 to 100.0.

Thanks for your help.

4

1 回答 1

4

像这样的模式应该有效:

^(0\.[1-9]|[1-9][0-9]{,2}\.[0-9])$

这将匹配 a 0.,后跟一个数字1to 9,或者一个数字 from 1to9后跟零,一个或两个数字0to 9,a.和另一个数字0to 9

另一种解决方案是使用前瞻:

^(?=.*[1-9])\d{1,3}\.\d$

这将匹配一到三个数字,后跟 a.和另一个数字,但前提是该序列包含一个数字 from 1to 9

但是,我建议将数字解析为小数并直接检查它的范围。

于 2013-10-01T19:39:18.820 回答