1

I need to match a pattern for validation . I want to match a decimal number where numeric part can have upto 14 digits including + or - without 0 and after decimal has upto 4 digits. Valid patterns are :

+1.23
9857.6543
-745290.0

Invalid patterns are:

 0
 0.00
 1.23456

I have tried ^[0-9]{0,14}\.[0-9]{0,4}$. I am not getting how to match for +,- and 0 condition

3
  • Is 12. or 12 valid? Commented Nov 10, 2021 at 1:45
  • and how about 0.0, 1.0, 1.00 Commented Nov 10, 2021 at 1:46
  • 12. is invalid , 12 is valid , 0.0 is invalid, 1.0 is valid, 1.00 is valid. Commented Nov 10, 2021 at 2:35

5 Answers 5

2

Short answer: ^[+-]?[1-9][0-9]{0,13}\.[0-9]{1,4}$


^ - start of string

[+-]? optionally, one between + and -

[1-9][0-9]{0,13} - a 14 digit number that doesn't start with 0

\. - decimal separator, has to be escaped or it will mean "any one character"

[0-9]{1,4} - up to 4 decimal digits

$ - end of string

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

1 Comment

I agree on the character class, thanks for pointing it out. Given their third valid example, I assumed that zeros are allowed as long as the entire integer part isn't 0
1

This might work ^(\+|-)?(([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})?|0{1,14}\.(?=0*[1-9])[0-9]{1,4})$

  • ^(\+|-)? - starts with +/-
  • (
  • ([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})? - absolute value >= 1
  • | - or
  • 0{1,14}\.(?=0*[1-9])[0-9]{1,4} - 0.**** with at least 1 non-zero digit
  • )
  • $ - end

const testcases = [
    '+1.23', 
    '9857.6543', 
    '-745290.0', 
    '1.0', 
    '1.00', 
    '12', 
    '0.01', 
    '+001.01', 
    '0', 
    '0.00', 
    '1.23456', 
    '0.0', 
    '12.'];
 
const regex = /^(\+|-)?(([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})?|0{1,14}\.(?=0*[1-9])[0-9]{1,4})$/;

testcases.forEach(n => console.log(`${n}\t - ${regex.test(n)}`));

Comments

1

The pattern:

^[+-]?[^\D0]\d{0,13}\.\d{1,4}(?!\d)

matches the first 3 but not the second 3. [^\D0] is, if I'm not mistaken, strictly the same as [123456789], but slightly more compact.

Comments

1

You can assert that the string does not start with 1 or more zeroes, followed by an optional dot and optional zeroes.

Then match 1-14 digits and optionally a dot and 1-4 digits, which would also allow for 00001 for example

^(?!0+\.?0*$)[+-]?\d{1,14}(?:\.\d{1,4})?$

The pattern matches:

  • ^ Start of string
  • (?!0+\.?0*$) Negative lookahead, assert not 1+ zeroes, optional dot and optional zeroes
  • [+-]? Optionally match + or -
  • \d{1,14} Match 1-14 digits
  • (?:\.\d{1,4})? Optionally match . and 1-4 digits
  • $ End of string

Regex demo

Comments

0

How about this?

^(?!0\.?0*$)[\+\-]?[0-9]{1,14}\.?[0-9]{0,4}$

Note: this does not match decimals without a leading 0 like .123 and does not match numbers with spaces in between the + or -. Though, these could be added to the pattern.

Requiring a non-zero digit before a . eliminates fractions of whole numbers, like 0.123.

Also, if you don't want integers and only want decimals, you will need to modify to make the . required:

^(?!0\.?0*$)[\+\-]?[0-9]{1,14}\.[0-9]{0,4}$

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.