0

I have a text box for user input where user can enter only comma separated numbers. Comma at the end of the string is also allowed

I am done with regex for that

var regex=/^[0-9\,]+$/;

How to apply higher bound limit on each comma separated number?

lets say, maximum number is 10 digit

following is valid

11,22,333,555555 and

11,23,

following is invalid

111111111111111111111111,4,4

jsfiddle here

0

1 Answer 1

1

Use the following pattern:

var regex = /^[0-9]{1,10}(?:,[0-9]{1,10})*$/;

This says to match any number of 1 to 10 digits, followed by a comma and another 1-10 digit number, this quantity zero or more times.

Demo

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

7 Comments

Replace + with {1,10} to limit each number to 10 digits.
Thank you. thats exactly i am looking for
Hey, in the above, if comma is given it is expecting another digit to be followed. can we avoid that. for example 111, should also be positive.
Yes, because I assume that 123, is an invalid number. Is this not the case?
now only 111 allowed 111, is not allowed. as commas allowed by design can i have numbers and any number of commas
|

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.