1

I require a regex to validate fixed range decimal numbers e.g. 1234.1234 - valid, 4444.1234 - valid 123.123 - invalid, 1234.123 - invalid

The number 4 digit before decimal and 4 digit after decimal only valid. I'm currently uses this regex - /^\S((\d{4})((\.\d{4})?))$/ but this not satisfies me.

2
  • did you want to match whole numbers? Commented Jul 15, 2015 at 7:14
  • why regex? it might be both faster and safer to just parse the number and check the range using numerical comparison. Commented Jul 15, 2015 at 7:15

2 Answers 2

2
^\d{4}(\.\d{4})?$

This should do it for you.Use

^[1-9]\d{3}(\.\d{4})?$

If you dont want to match 0234.1234

Sign up to request clarification or add additional context in comments.

1 Comment

I modified your regex - ^\d{4}(\.\d{4})?$ to new - ^\d{4}\.\d{4}$ for matching only the number 4 digit before decimal and 4 digit after decimal. I think this is correct one. what is your suggestion?
0

You can use this regex:

/^\d{4}(?:\.\d{4})?$/

This will match 1234 or 1234.5678 as valid matched.

RegEx Demo

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.