How to make type=“number” to positive numbers only

后端 未结 17 2289
既然无缘
既然无缘 2020-11-30 17:12

currently I have the following code


it comes out to something like this

相关标签:
17条回答
  • 2020-11-30 18:08

    Try this:

    • Tested in Angular 8
    • values are positive
    • This code also handels null and negitive values.
                  <input
                    type="number"
                    min="0"
                    (input)="funcCall()" -- Optional
                    oninput="value == '' ? value = 0 : value < 0 ? value = value * -1 : false"
                    formControlName="RateQty"
                    [(value)]="some.variable" -- Optional
                  />
    
    0 讨论(0)
  • 2020-11-30 18:11

    <input type="number" min="1" step="1">

    0 讨论(0)
  • 2020-11-30 18:13

    Add a min attribute

    <input type="number" min="0">

    0 讨论(0)
  • 2020-11-30 18:14

    I have found another solution to prevent negative number.

    <input type="number" name="test_name" min="0" oninput="validity.valid||(value='');">
    
    0 讨论(0)
  • 2020-11-30 18:16

    It depends on how precise you want to be. It you want to accept only integers, than:

    <input type="number" min="1" step="1">

    If you want floats with, for example, two digits after decimal point:

    <input type="number" min="0.01" step="0.01">

    0 讨论(0)
提交回复
热议问题