-6

Please can you help me, I need a regex expressions to only allow whole numbers and the string cannot be blank.

Thanks

Wesley
  • 87
  • 2
  • 5

2 Answers2

5
/^-?\d+$/

This regex would match one or more digits. So won't allow blank as required, and would only allow whole numbers
Actually the + after \d imposes that at least one digit be present in the input string.
The - in the beginning checks for a -, and the following ? makes it optional.

Ali Shah Ahmed
  • 3,263
  • 3
  • 24
  • 22
1

You should try this one:

^\d+$

^ : begining of the string to capture
$ : end of the string to capture
\d : all the digits (equivalent to [0-9])
+ : at least one of the class

pierroz
  • 7,653
  • 9
  • 48
  • 60