-3

I want to check whether given input is valid integer range.

[0-9]-{1}[0-9]

The above regex not working for some cases.

Example:
10 // false. - working as expected
10-20 // true. - working as expected
h-g // false. - working as expected
10-20- // true. - should be false
10-20-30 // true. - should be false

Update 2: Check for comma(,) separated input

Now the same input text field can allow comma(,) separated input as well. Eg. 10,20,30 also allowed. rangePattern = new RegExp('^[0-9]*,[0-9]*$'); is not allowing me to give more than one comma. How to allow repentance.

Example:
10,20 valid.
10,20,30 valid.
10,20, invalid.
10,20-30 invalid.

Update 3: regex /^(\d+,)*\d+$/ not working for input 10,,,20 How to resolve this?

Solved:
{1} - allow only once.
regex should be /^(\d+,{1})*\d+$/

Suresh Rajagopal
  • 183
  • 1
  • 3
  • 21
  • In addition to the examples, you might wanna define (in words) what the expected range is...for example, negative number to positive number `-10-20`? what about `20-10`? – CrayonViolent Aug 06 '15 at 04:01
  • the problem statement increased complexity by allowing float number as well. I have modified the regex to allow integer / float numbers `/^(\s*\d+|\d*\.\d+\s*,{1})+\s*\d+|\d*\.\d+\s*$/`. This is working for all type of input except one certain format. Example. for input 1,2.5,3 it fails. Actually it should pass. – Suresh Rajagopal Aug 17 '15 at 09:38

1 Answers1

2

Your regex only check for substring/contains.

Use anchors ^: starts with and $: ends with.

/^\d+-\d+$/

Explanation:

  1. /: Delimiters of regex
  2. ^: Starts with
  3. \d+: Matches one or more numbers
  4. -: Matches - literal
  5. $: Ends with

jsfiddle Demo

<input onblur="alert(/^\d+-\d+$/.test(this.value))">

RegEx101 Demo


Update

var rangePattern = new RegExp('/^[0-9]*-[0-9]*$/');
pass = rangePattern.test(rangeValue);

You don't need / delimiters when you use RegExp constructor.

Use

var rangePattern = new RegExp('^[0-9]*-[0-9]*$');

Update 2

To match comma-separated numbers:

/^(\d+,+)*\d+$/g

RegEx101 Demo

Tushar
  • 85,780
  • 21
  • 159
  • 179
  • 1
    i would also point out that `*` quantifier means 0 or more so e.g. `-` `2-` `-2` all match. Also so does `100-10`. I mostly blame OP's lack of clarity on requirements though. – CrayonViolent Aug 06 '15 at 04:05
  • var rangePattern = new RegExp('/^[0-9]*-[0-9]*$/'); pass = rangePattern.test(rangeValue); it returns false for input: 10-20 – Suresh Rajagopal Aug 06 '15 at 04:10
  • I think this counts as the most gratuitous use of an un–asked for library in an answer. e.g. demo ``. – RobG Aug 06 '15 at 04:11
  • 1
    @RobG That is just for Live Demonstration purpose – Tushar Aug 06 '15 at 04:13
  • The comment I posted can be placed as a live demo too. In one line. ;-) – RobG Aug 06 '15 at 04:14
  • excellent.. working fine. Since I used `new RegExp`, was not working, now used your code snippet. working as expected. thanks a lot @Tushar – Suresh Rajagopal Aug 06 '15 at 04:15
  • @CrayonViolent No, these are all invalid. You can also check this in [demo](http://jsfiddle.net/tusharj/7tyg7dqd/) – Tushar Aug 06 '15 at 04:19
  • Now the same input text field can allow comma `,` separated input as well. Eg. 10,20,30 also allowed. `rangePattern = new RegExp('^[0-9]*,[0-9]*$')`; is not allowing me to give more than one comma. How to allow repentance. Eg. 10,20 valid. 10,20,30 valid. 10,20, invalid. 10,20-30 invalid. – Suresh Rajagopal Aug 06 '15 at 04:31
  • 1
    @SureshRajagopal `^(\d+,)*\d+$` See [Demo](https://regex101.com/r/kU1mL7/1) – Tushar Aug 06 '15 at 04:34
  • @Tushar.. for input 10,,,20, it is not working. How to resolve this? – Suresh Rajagopal Aug 06 '15 at 07:05
  • @SureshRajagopal add + after , in regex. `^(\d+,+)*\d+$` – Tushar Aug 06 '15 at 07:07
  • @SureshRajagopal You should create another question instead of updating the same again and again as your request for `,` separated is completely different than the first question – Tushar Aug 06 '15 at 07:08
  • @SureshRajagopal OR you could actually write out what you actually want the pattern to match, since you keep adding requirements to it – CrayonViolent Aug 06 '15 at 13:13