2

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 complain 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 and

2
  • it can be some time but not necessary that i need to have in floating format.. thanks Dec 11, 2014 at 4:42
  • 1
    did you want to allow 0.00 or 00.00? Dec 11, 2014 at 4:44

3 Answers 3

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

You can simply use this.See demo.

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

6
  • This matches 00.00 but i dont want this also 0.00,0,00.00,00.0. these have got no meaning. Dec 11, 2014 at 4:46
  • but these values are also acceptable : 2, 2.5, 2.00, 2.01. basically upto 99.99 is acceptable. i am sorry if i had confused you with my question. Dec 11, 2014 at 4:55
  • This matches 0.0,00.0 0.0 00.0 Dec 11, 2014 at 5:05
  • .85,.10 these are also valid values but the above one does not accept this, could you please look in to... but if i enter 0.58 or 0.25 or 0.85 it accepts.. Dec 11, 2014 at 5:12
  • Thanks for the patience and help.. it works now as i expected. very kind of you...pls give me some times will check with few more values and post here the results Dec 11, 2014 at 5:15
3

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

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
2
  • 1
    Definitely the right approach, but parseFloat may not be ideal because it will ignore cruft at the end. Also, he may not want to truncate the decimal portion to two digits, but rather only accept input with two digits.
    – user663031
    Dec 11, 2014 at 5:16
  • @torazaburo the second function is based on what he said. I am trying to get the value in a 00.00 format, with the second one parseFloat can't do anything tricky with the number because its formatted to 00.00 before its placed in a parseFloat
    – Jay Harris
    Dec 11, 2014 at 5:21
1

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

4
  • but these values are also acceptable : 2, 2.5, 2.00, 2.01. basically upto 99.99 is acceptable. i am sorry if i had confused you with my question Dec 11, 2014 at 4:53
  • yep it allows 2.00, 2.01. That's what you asked.. GReater than 0.00 upto 99.99. Could you explain your question briefly. Dec 11, 2014 at 4:56
  • it does not allow 2, 3, 5 which are valid. Dec 11, 2014 at 4:57
  • just make the decimal part optional. See my update. \.\d{1,2} allows one or two digits after the decimal part. Dec 11, 2014 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.