0

I'm currently working with the regex to only allow users to input digits xxxx.xxx or xxxx.xx or xxxx.

I tried the below code but instead of allowing the 5 digits, it also allows 7 and 8 digits.

(^\d{5}$)|(^\d{4}.\d{2}$|^\d{4}.\d{3}$)
^[0-9]{4}.([0-9]{3}|[0-9]{2}|[0-9]{5})$

Sample output: 1223.345 or 1324.23 or 23564

Advance Thanks!

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

0

Your period needs to be escaped, or else it will be the "any character" symbol, and will allow a digit in that place. So for that part of your regex, try:

/^\d{5}$|^\d{4}\.\d{2}$|^\d{4}\.\d{3}$/;

I'm not sure what the remainder of your regex is for, but seems outside the scope of what you're asking.

pwilcox
  • 5,542
  • 1
  • 19
  • 31
  • Hi pwilcox, (^\d{5}$)|(^\d{4}\.\d{2}$|^\d{4}\.\d{3}$) - this is working. Thanks a lot for your help. Really appreciate it – Romel Pertubal Mar 27 '20 at 21:23
  • Changed it I think while you were commenting. I believe it's pretty much what you have without the parentheses. If it works for you, that's great. Consider accepting the answer (checkmark). – pwilcox Mar 27 '20 at 21:25
  • You can shorten `^\d{4}\.\d{2}$|^\d{4}\.\d{3}$` to simply `^\d{4}\.\d{2,3}$` – Gary Mar 27 '20 at 21:35