javascript - Validate comma separated numbers - Stack Overflow

I have to check if a string has this pattern: 1,2,10,11.The rules are:It may be a sequence like: 1 or 1

I have to check if a string has this pattern: 1,2,10,11.

The rules are:

  • It may be a sequence like: 1 or 1,2 or 1,2,n+1. Many numbers user wants;
  • The number must be followed by a ma but can't ends with it, like: 1,;
  • The number can have 1 or 2 digits;
  • No other character but number and ma, no space or whatever;
  • No need to check numbers sequence, this is not the point.

What I have tried is:

  • (\d{1,2})(,): This code checks for a 1, sentence and returns true no metters what is on the rest of the string, like 1,afasfs;
  • I've tried look ahead: (\d{1,2})(?=(,)) but it accepts the above case too;
  • The both cases above I can't apply ^ and $ because it fails on right scenarios like: 1,2,3.

What I don't know is how to test the entire string. All my tests only checks a part of it(e.g 1,) not the entire string.

Note: I'm using JavaScript test() function. Don't know if is the right one, but I believe that it is.

I have to check if a string has this pattern: 1,2,10,11.

The rules are:

  • It may be a sequence like: 1 or 1,2 or 1,2,n+1. Many numbers user wants;
  • The number must be followed by a ma but can't ends with it, like: 1,;
  • The number can have 1 or 2 digits;
  • No other character but number and ma, no space or whatever;
  • No need to check numbers sequence, this is not the point.

What I have tried is:

  • (\d{1,2})(,): This code checks for a 1, sentence and returns true no metters what is on the rest of the string, like 1,afasfs;
  • I've tried look ahead: (\d{1,2})(?=(,)) but it accepts the above case too;
  • The both cases above I can't apply ^ and $ because it fails on right scenarios like: 1,2,3.

What I don't know is how to test the entire string. All my tests only checks a part of it(e.g 1,) not the entire string.

Note: I'm using JavaScript test() function. Don't know if is the right one, but I believe that it is.

Share Improve this question asked Dec 2, 2013 at 20:25 DontVoteMeDownDontVoteMeDown 21.5k10 gold badges72 silver badges113 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can try this regex:

/^(\d{1,2},)*\d{1,2}$/

Details:

^           - Line start
\d{1,2}     - 1 or 2 digit number
\d{1,2},    - 1 or 2 digit number followed by a ma
(\d{1,2},)* - 0 or more of 1/2 digit number followed by a ma character
\d{1,2}     - 1 or 2 digit number
$           - Line end

per OP

The number must be followed by a ma

so a little modification to anubhava's pattern/^(\d{1,2},)+\d{1,2}$/ otherwise it will validate single or double digits only like 1 or 10

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745223895a4617372.html

相关推荐

  • javascript - Validate comma separated numbers - Stack Overflow

    I have to check if a string has this pattern: 1,2,10,11.The rules are:It may be a sequence like: 1 or 1

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信