1

I have an HTML input that accepts decimal numbers starting from 0.001. The problem is when the user clicks the down arrow it starts from -0.001. How can I avoid the negative ??

<input type="number" name="amount" placeholder="0.001" step="0.001"  required>
mrghofrani
  • 1,335
  • 2
  • 13
  • 32
steveyout
  • 159
  • 3
  • 13
  • First result: [Google search your question](https://www.google.com/search?q=how+to+avoid+negative+numbers+in+html+input) – Bastian Springer Mar 18 '20 at 12:06
  • You have wonderfull attributes to set a min value... – Dorvalla Mar 18 '20 at 12:09
  • 4
    Does this answer your question? [Is there any way to prevent input type="number" getting negative values?](https://stackoverflow.com/questions/7372067/is-there-any-way-to-prevent-input-type-number-getting-negative-values) – Kundan Mar 18 '20 at 12:12

4 Answers4

2

Set the attribute min:

<input type="number" name="amount" min="0.001" placeholder="0.001" step="0.001"  required>

JSFiddle: https://jsfiddle.net/6spubgjm/

Suboptimierer
  • 554
  • 4
  • 11
2

You can add a min attribute of 0 to fix this.

<input type="number" name="amount" placeholder="0.001" step="0.001" min="0" required>

<input type="number" name="amount" placeholder="0.001" step="0.001" min="0" required>
Roy
  • 7,811
  • 4
  • 24
  • 47
1

<input type="number" name="amount" placeholder="0.001" step="0.001" min="0.001" required>
mrghofrani
  • 1,335
  • 2
  • 13
  • 32
Bastian Springer
  • 271
  • 2
  • 11
1

You can use min property

<input type="number" min="0" name="amount" placeholder="0.001" step="0.001"  required>
Ali Asgher Badshah
  • 811
  • 1
  • 11
  • 34