Javascript – regex for 6 digits before and 2 digits after decimal

javajavascriptregex

I need a regex in which decimal is optional. If decimal is there then there can be max 6 digits before and max 2 digits after decimal. If decimal is not there then max of 6 digits is valid.

Regex tested :^\d{0,6}\.?\d{1,2}$

The above regex allows max of 8 digits without decimal. How can I change according to my needs so that if there is no decimal then it would take max of 6 digits?

VALID CASES

123456.12  
21231  
123456  
15465.43  
23.34  
6.45  
.12

INVALID CASES

12345678  
123456.331  

Best Solution

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

Try this.See demo.

https://regex101.com/r/oL9kE8/4

Just make the (\.\d{1,2}) decimal part optional.?

Related Question