0

I want to validate a text field to accept just text like this :

  • 1,2;2,3;1-3
  • 1-2;4;2,3;4;1-3
  • 12

I don't want the types like this :

  • ;1
  • ,1
  • -1
  • 1;;2
  • 1,,2
  • 1--2
  • 1-2-3
  • 1,2,3
  • 1,2-3

so I make this regular expression but it seems doesn't work like what I want

var reg = /^\d*(((?!.*--)(?!.*,,)(?!.*;;)(?!.*,;)(?!.*,-)(?!.*-;)(?!.*-,)(?!.*;,)(?!.*;-))[,-;])*\d$/

thanks for your help :)

3 Answers 3

1

you can simply use the regex

function match(str){
    return str.match(/^(?!.*([-,])\d+\1)(?!.*,\d+-)\d+(?:[-,;]\d+)*$/) != null
}

console.log(match(';1'));
console.log(match(',1'));
console.log(match('1;;2'));
console.log(match('1-3'));
console.log(match('12'));
console.log(match('1,2;2,3;1-3'));
console.log(match('1-2;4;2,3;4;1-3'));
console.log(match('1,2,3'));

take a look at regex demo

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

1 Comment

thank you @marvel308 for your response ,is there any way to elimanate this case 1,2,3?
1

Here's my attempt. Based on your examples I've assumed that semi-colons are used to separate 'ranges', where a 'range' can be a single number or a pair separated by either a comma or a hyphen.

var re = /^\d+([,\-]\d+)?(;\d+([,\-]\d+)?)*$/;

// Test cases
[
    '1',
    '1,2',
    '1-2',
    '1;2',
    '1,2;2,3;1-3',
    '1-2;4;2,3;4;1-3',
    '12',

    ';1',
    ',1',
    '-1',
    '1;;2',
    '1,,2',
    '1--2',
    '1-2-3',
    '1,2,3',
    '1,2-3'
].forEach(function(str) {
    console.log(re.test(str));
});

The first part, \d+([,\-]\d+)? matches a 'range' and the second part (;\d+([,\-]\d+)?)* allows further 'ranges' to be added, each starting with a semi-colon.

You can add in ?: to make the groups non-capturing if you like. That's probably a good idea but I wanted to keep my example as simple as I could so I've left them out.

Comments

1

You may use

/^\d+(?:(?:[-,;]\d+){3,})?$/

See the regex demo

Details

  • ^ - start of string
  • \d+ - 1 or more digits
  • (?:(?:[-,;]\d+){3,})? - 1 or 0 sequences of:
    • (?:[-,;]\d+){3,} - 3 sequences of:
      • [-,;] - a -, , or ;
      • \d+ - 1 or more digits
  • $ - end of string

var ss = [ '1,2;2,3;1-3','1-2;4;2,3;4;1-3','12',';1',',1','-1','1;;2','1,,2','1--2','1-2-3','1,2,3','1,2-3',';1',',1','-1','1;;2','1,,2','1--2' ];
var rx = /^\d+(?:(?:[-,;]\d+){3,})?$/;
for (var s of ss) {
  console.log(s, "=>", rx.test(s));
}

NOTE: the [,-;] creates a range between , and ; and matches much more than just ,, - or ; (see demo).

2 Comments

thank you @Wiktor for your explanation, it works fine. I want to eliminate this case too 1,3,4
@OLH: so, you want to match 1, 1,3, or 1,3,4,5 but not 1,3,4? Please explain the rule. Or do you want to avoid matching delimited numbers if they are separated with only 1 type of separator (i.e. if there is a separator, there must be at least 2 different ones)?

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.