2

I would like to allow 0 or positive integers as input. The best solution I saw was

/^[0-9]+$/.test(input)

but this expression will allow the user to use "010", so I changed the expression to

/^(0|[1-9]+[0-9]*)$/.test(input)

Is that correct? Does this expression handle every input?

user3719454
  • 994
  • 1
  • 9
  • 24
  • 1
    There are a lot of regex testers put there, try this one to test it out with some test input: http://regexhero.net/tester/ – Srb1313711 Feb 24 '15 at 10:13

1 Answers1

1

That expression says

  • 0, or
  • 1-9 followed by 0 or more 0-9

Here's a visualization: Link.

so yes, that should cover all non-negative integers. Whether or not regular expressions is the right tool for the job is debatable.

aioobe
  • 413,195
  • 112
  • 811
  • 826