admin管理员组

文章数量:1291757

I am trying to enter value greater than 0 and less than 100 and i am using the following regex:

/^(0+)?(99(\.99?)?|(\.99?)?|\.[0-9]+|0?[0-9](\.[0-9]{0,2})?|(0+)?[0-9][0-9](\.[0-9][0-9]?)?)$/

this regex does not plain about zero but 0.01 is a valid value.

Basically, I am trying to get the value in a 00.00 format and it is also acceptable to have values like 0.01, 00.01, ... 99.99 but strictly numbers and dot only.

I am using javascript and html

I am trying to enter value greater than 0 and less than 100 and i am using the following regex:

/^(0+)?(99(\.99?)?|(\.99?)?|\.[0-9]+|0?[0-9](\.[0-9]{0,2})?|(0+)?[0-9][0-9](\.[0-9][0-9]?)?)$/

this regex does not plain about zero but 0.01 is a valid value.

Basically, I am trying to get the value in a 00.00 format and it is also acceptable to have values like 0.01, 00.01, ... 99.99 but strictly numbers and dot only.

I am using javascript and html

Share Improve this question edited Dec 11, 2014 at 4:50 Adam Jensen 5411 gold badge10 silver badges25 bronze badges asked Dec 11, 2014 at 4:38 Java QuestionsJava Questions 7,96343 gold badges119 silver badges177 bronze badges 2
  • it can be some time but not necessary that i need to have in floating format.. thanks – Java Questions Commented Dec 11, 2014 at 4:42
  • 1 did you want to allow 0.00 or 00.00? – Avinash Raj Commented Dec 11, 2014 at 4:44
Add a ment  | 

3 Answers 3

Reset to default 4
^(?!0?0\.00$)\d{1,2}\.\d{2}$

You can simply use this.See demo.

https://regex101./r/qB0jV1/10

So why do you have to beat your self up with a plex regex. Why not a number parison?

function validNum (num) {
   num = parserFloat(num);

   return num > 0 && num < 100;
}

Want only two decimal places ?

function validNum (num) {
   num = parseFloat((num + "").replace(/^(.*\.\d\d)\d*$/, '$1'));

   return num > 0 && num < 100;
}

Test Here

.5:      true
..5    : false
.005   : false
0.05   : true
99.99  : true
00.01  : true
00.00  : false
0.00   : false
00.0   : false
100.00 : false

I think you don't want to allow 0.00 or 00.00

^(?!00?\.00$)\d{1,2}(?:\.\d{2})?$

OR

^(?!00?\.00$)\d{1,2}(?:\.\d{1,2})?$

DEMO

Negative lookahead at the start (?!00?\.00$) asserts that the number going to match wouldn't be 00.00 or 0.00

本文标签: javascriptRegex to allow decimal numbers greater than 0 and less than 100Stack Overflow