-3

I am trying to create an input regex that will only accept whole, positive numbers. So far I have:

[^[+]?\d+([.]\d+)?$]

But users can still enter, say, .4. How can I alter this to disallow floats? Thanks!

Ron
  • 69
  • 1
  • 10
  • 1
    but your current regex `\d+([.]\d+)?` allows floats , actually – RomanPerekhrest Feb 23 '17 at 11:49
  • Are you wanting to disallow floats altogether, or accept whole positive numbers eg 123.00 ? – Gavin Jackson Feb 23 '17 at 12:01
  • yes, I would like to disallow floats altogether. So 123 would be accepted but 123.00 would not. The input field is for a number of people, so it would be odd for someone to enter in 123.00 – Ron Feb 23 '17 at 13:13

3 Answers3

1

This allows only positive numbers

^[0-9]+$
tak3shi
  • 2,305
  • 1
  • 20
  • 33
1

I think you try to filter only positive integers.

^[1-9]\d*$

allows only positive integer numbers. What is the regex for "Any positive integer, excluding 0" will also help you.

Community
  • 1
  • 1
javasenior
  • 1,786
  • 2
  • 22
  • 26
-1

Use only ^[+]?\d+$

you can see the test cases here

Abdul Hameed
  • 1,025
  • 12
  • 27