2

I am trying to use text input (not number) to do number validation for a condition of more than 0. So, it should not accept inputs -1, -2, 0. But 1, 100, 2000, 4000000.. possibly do a limit on it.

Is there a pattern to do something like that using HTML5 validation ? (not js/jq just HTML5 validation)

Here is what I tried but does not fulfill completely my requirements:

  • this works perfectly but I dont want to use number type but "text"

http://jsfiddle.net/5Krwy/

<input id="quantity" name="quantity" type="number" min="1" max="100000" required />
  • now this uses the type text I want, but it can still accept 0. How to improve on this pattern ?

http://jsfiddle.net/5Krwy/1/

<input id="quantity" name="quantity" type="text" pattern = "[0-9]{1,5}" required />
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85
Axil
  • 3,606
  • 10
  • 62
  • 136
  • Possible duplicate of [How to make type="number" to positive numbers only](https://stackoverflow.com/questions/19233415/how-to-make-type-number-to-positive-numbers-only) – rofrol Aug 29 '17 at 14:01

2 Answers2

9

You can try this:

<input id="quantity" name="quantity" type="text" pattern = "[1-9][0-9]{0,4}" required />
Femi
  • 1,332
  • 9
  • 20
1

obviously, you should change the patten attribute

<input id="quantity" name="quantity" type="text" pattern = "[1-9]{1,5}" required />
Yubin Duan
  • 53
  • 4