Your regular expression: @^(?:[1-9][0-9]*)| 0$@ @^(?:[1-9][0-9]*)| 0$@
- says that they correspond to those numbers that begin with
1-9 , followed by any digit 0 or more OR - correspond to those numbers that end in
0 .
Clearly, -0 satisfies condition 2, so you get a match.
The regular expression you need: @^(?:[1-9][0-9]*|0) $@
which matches 0 or any other +ve number without a leading 0 . If leading 0 is enabled, all you need to check is that the input contains numbers for which you can use: ^\d+$ , as Mark points out.
Also, why not just do a simple check:
if($input >= 0) // $input is non -ve.
codaddict
source share