Regex validation for numbers with comma separator

前端 未结 3 1343
情深已故
情深已故 2020-12-03 18:44

Need a regular expression to validate number with comma separator. 1,5,10,55 is valid but 1,,,,10 is not valid.

3条回答
  •  执笔经年
    2020-12-03 19:27

    This should do it:

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

    The regex is rather simple: \d+ is the first number, followed by optional commas and more numbers.

    You may want to throw in \s* where you see fit, or remove all spaces before validation.

    • To allow negative numbers replace \d+ with [+-]?\d+
    • To allow fractions: replace \d+ with [+-]?\d+(?:\.\d+)?

提交回复
热议问题