2

I am working on one input application where i need to test input values that accepts single, multiple and even a range of numbers .

Eg inputs : 70,900,80-20 // should return true as all are valid
as,@123 // should return false as it is not valid digit
12-123-12123-123123 // should also return false

I am trying to use this in regex. I have tried this.

/^[\d,\-{1}]+/

I am not able to test using this regex. Let me know where i am doing wrong

6
  • The rule regarding hyphens is that there can only be one but it can be followed by any number of integers? ^\d+(-?\d+)?$ Commented Jul 3, 2019 at 17:48
  • Which characters are valid? or simply put more examples. Commented Jul 3, 2019 at 17:48
  • @hungerstar Only one hyphen can be add . It just simple like number range for eg : 8-20 but this should not be valid 8-20-80 Commented Jul 3, 2019 at 17:51
  • @mrReiha only single hyphen and commas are allowed. Commented Jul 3, 2019 at 17:52
  • take a look at this -> /^\d+-?\d*$/ Commented Jul 3, 2019 at 17:56

2 Answers 2

5

This regular expression should work for you:

/^\d+(-\d+)?(,\d+(-\d+)?)*$/

Explanation:

  • ^ means to start at the beginning of the string, to reject strings with extra stuff at the beginning
  • \d+(-\d+)? accepts a number optionally followed by a hyphen and another number:
    • \d+ means one or more digits
    • -\d+ means hyphen plus one or more digits
    • (...)? means the pattern inside the parentheses is optional
  • (,...)* accepts 0 or more instances of comma followed by the same pattern as above
  • $ says to match until the end of the string, to reject strings with extra stuff at the end
Sign up to request clarification or add additional context in comments.

3 Comments

It works like charm. Can you explain this regex more. I didnt understand.
Just added more details to the explanation. Is it clear enough now? I can augment more if needed.
Thanks understood
2

/^\d+(-\d+)?(,\d+(-\d+)?)*$/

const input = document.querySelector( 'input' );
const msg   = document.querySelector( '.msg' );
const regex = /^\d+(-\d+)?(,\d+(-\d+)?)*$/;

input.addEventListener( 'keyup', function ( e ) {
  const str = regex.test( this.value ) ? 'Match!' : 'No Match';
  msg.textContent = str;
} );
<input type="text" name="numbers">
<div class="msg"></div>

5 Comments

Did not realize commas need to be included, updating.
its not satisfying even base condition. If i have multiple values eg : 1,12 . it is not matching.
@ShubhamVerma did you read my comment? You should also clarify that commas must be included. It's not obvious.
Nice code around the RE. I'd suggest removing ? after - since it's superfluous, but it also already works as is.
@joanis thanks, I do what I can with regex as I'm no pro.

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.