Float Regex format for a 2 decimal place number between 0 and 100 [duplicate]

I need to validate a float number with 2 decimal place digits.the number should be between 0 - 100. (99.99 inclusive) But don't know how to do it. Please guide me through it. Javascript/jQuery both are fine. thanks.

Answers:

Answer

If you want to match a number between 0 - 100 and contains also two decimals, you can use a regex like this:

^(100(?:\.00)?|0(?:\.\d\d)?|\d?\d(?:\.\d\d)?)$

Working demo

enter image description here

Answer

^\d{1,2}(\.\d{,2})?$, assuming you're guaranteed that this is the only thing going into your input box.

Answer

The regex to validate a number between 0 - 100 (exclusive of 100) with two decimal digits is

^[1-9]?\d\.\d\d$

E.g.

console.log( /^[1-9]?\d\.\d\d$/.test('0.10') );    // true

If the decimal digits are optional (i.e. there may be 0, 1, or 2 decimal digits) instead use

^[1-9]?\d(\.\d\d?)?$

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.