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

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

currently I have the following code


it comes out to something like this

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

    If needing text input, the pattern works also

    <input type="text" pattern="\d+">
    
    0 讨论(0)
  • 2020-11-30 17:52

    I cannot find the perfect solution as some work for inputting but not for copy&paste, some are the other way around. This solution works for me. It prevents from negative number, typing "e", copy&paste "e" text.

    create a function.

    <script language="JavaScript">
    
      // this prevents from typing non-number text, including "e".
      function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        let charCode = (evt.which) ? evt.which : evt.keyCode;
        if ((charCode > 31 && (charCode < 48 || charCode > 57)) && charCode !== 46) {
          evt.preventDefault();
        } else {
          return true;
        }
      }
    </script>
    

    add these properties to input. this two prevent from copy&paste non-number text, including "e". you need to have both together to take effect.

    <input type="number" oninput="validity.valid||(value='');" onpress="isNumber(event)" />
    

    If you are using Vue you can refer this answer here. I have extracted it to a mixin which can be reused.

    0 讨论(0)
  • 2020-11-30 17:52

    It depends on whether you want an int or float field. Here's what the two would look like:

    <input type="number" name="int-field" id="int-field" placeholder="positive int" min="1" step="1">
    <input type="number" name="float-field" id="float-field" placeholder="positive float" min="0">
    

    The int field has the correct validation attached to it, since its min is 1. The float field accepts 0, however; to deal with this, you can add one more constraint validator:

    function checkIsPositive(e) {
      const value = parseFloat(e.target.value);
      if (e.target.value === "" || value > 0) {
        e.target.setCustomValidity("");
      } else {
        e.target.setCustomValidity("Please select a value that is greater than 0.");
      }
    }
    
    document.getElementById("float-field").addEventListener("input", checkIsPositive, false);
    

    JSFiddle here.

    Note that none of these solutions completely prevent the user from trying to type in invalid input, but you can call checkValidity or reportValidity to figure out whether the user has typed in valid input.

    Of course, you should still have server-side validation because the user can always ignore client-side validation.

    0 讨论(0)
  • 2020-11-30 17:54

    You can force the input to contain only positive integer by adding onkeypress within the input tag.

    <input type="number" onkeypress="return event.charCode >= 48" min="1" >

    Here, event.charCode >= 48 ensures that only numbers greater than or equal to 0 are returned, while the min tag ensures that you can come to a minimum of 1 by scrolling within the input bar.

    0 讨论(0)
  • 2020-11-30 17:56

    If you try to enter a negative number, the onkeyup event blocks this and if you use the arrow on the input number, the onblur event resolves that part.

    <input type="number" 
        onkeyup="if(this.value<0)this.value=1"
        onblur="if(this.value<0)this.value=1"
    >

    0 讨论(0)
  • 2020-11-30 17:57

    You can turn negative input into a positive number by the following:

    <input type="number" onkeyup="if(this.value<0){this.value= this.value * -1}">

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