regex

Regular expression to accept negative number


I have this regular expression below:

^([0-9]+,)*[0-9]+$

This will validate the input from a user to accept positive or negative numbers only that is comma separated or dash.

123,123,10
10,10
25
-25,10

My regular expression above is only working for positive numbers and comma separator. How can I modify this to work with dash (10-25-30) and negative numbers?


Solution

  • You can use this regex:

    ^([-+]?[0-9]+[-,])*[+-]?[0-9]+$
    

    RegEx Demo

    RegEx Details: