1

I am doing amount validation. The requirements are:

  1. There should not be a leading zero.
  2. It should accept only numbers.
  3. There should be a single decimal point.
  4. it should accept 9 digits before decimal and 2 digits after decimal.

I have tried this regex:

/^(([1-9]\d{0,8})(\.\d{1,2})?)/g;

The issue is, after entering 9 digits, decimal is getting entered only if you enter any digits along with it simultaneously (at a time), without it I am unable enter decimal point.

9
  • You want to be able to enter a decimal without a number following it? Commented Feb 17, 2016 at 18:25
  • So you want to allow 123. as valid input Commented Feb 17, 2016 at 18:26
  • 2
    A \d{0,2} should allow that. Commented Feb 17, 2016 at 18:27
  • yes absolutely .. not simultaneously.First i should be able to enter decimal point,then the numbers Commented Feb 17, 2016 at 18:27
  • @Aishwarya then what kenney said should work for you... Commented Feb 17, 2016 at 18:27

1 Answer 1

1

This could help:

/^(?!0)\d{1,9}\.\d{0,2}$/

In this case, I used a lookahed ((!?0)) to prevent a leading zero, then used a similar OP expression to match the string. It means that everything in expression \d{1,9}\.\d{0,2} and not preceded by a zero will be matched.

Demo

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

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.