Joe
Joe

Reputation: 4532

JavaScript regex to accept values between 00.01 to 999.99999

I have the following regex

var pattern = /^(\d{2,3})?(?:\.\d{2,5})?$/;

//The value must be always lesser than 999.99999
if(!pattern.test(billRate.value)|| parseFloat(billRate.value) > parseFloat("999.99999"))
{
    alert("Invalid It accepts values between 00.01 to 999.99999");
    return false;
}

The minimum value should be always 00.01 and the max value is 999.99999.

Also the regex should always check that there is minimum of two numbers before decimal point and minimum of two decimals after.

Unfortunately the above regex is not working properly.

Upvotes: 0

Views: 134

Answers (1)

user557597
user557597

Reputation:

You can try this

^(?:0?00\.(?:01\d{0,3}|0[2-9]\d{0,3}|[1-9]\d{1,4})|(?:0?0[1-9]|0?[1-9]\d|[1-9]\d{2})\.\d{2,5})$

Unfortunately, I don't have a way to test it.

Readable version

 ^   

 (?:
      0? 00 \.                      #  000.01000 - 000.99999
      (?:
           01 \d{0,3} 
        |  0 [2-9] \d{0,3} 
        |  [1-9] \d{1,4} 
      )
   |  
      (?:                           #  001.00000 - 999.99999
           0? 0 [1-9] 
        |  0? [1-9] \d 
        |  [1-9] \d{2} 
      )
      \.
      \d{2,5} 
 )

 $

Upvotes: 1

Related Questions