How can I limit possible inputs in a HTML5 “number” element?

前端 未结 26 2531
感动是毒
感动是毒 2020-11-22 05:12

For element, maxlength is not working. How can I restrict the maxlength for that number element?

相关标签:
26条回答
  • 2020-11-22 05:24

    As with type="number", you specify a max instead of maxlength property, which is the maximum possible number possible. So with 4 digits, max should be 9999, 5 digits 99999 and so on.

    Also if you want to make sure it is a positive number, you could set min="0", ensuring positive numbers.

    0 讨论(0)
  • 2020-11-22 05:24
    <input type="number" onchange="this.value=Math.max(Math.min(this.value, 100), -100);" />
    

    or if you want to be able enter nothing

    <input type="number" onchange="this.value=this.value ? Math.max(Math.min(this.value,100),-100) : null" />
    
    0 讨论(0)
  • 2020-11-22 05:24

    As I found out you cannot use any of onkeydown, onkeypress or onkeyup events for a complete solution including mobile browsers. By the way onkeypress is deprecated and not present anymore in chrome/opera for android (see: UI Events W3C Working Draft, 04 August 2016).

    I figured out a solution using the oninput event only. You may have to do additional number checking as required such as negative/positive sign or decimal and thousand separators and the like but as a start the following should suffice:

    function checkMaxLength(event) {
    	// Prepare to restore the previous value.
    	if (this.oldValue === undefined) {
    		this.oldValue = this.defaultValue;
    	}
    
    	if (this.value.length > this.maxLength) {
    		// Set back to the previous value.
    		this.value = oldVal;
    	}
    	else {
    		// Store the previous value.
    		this.oldValue = this.value;
    		
    		// Make additional checks for +/- or ./, etc.
    		// Also consider to combine 'maxlength'
    		// with 'min' and 'max' to prevent wrong submits.
    	}
    }

    I would also recommend to combine maxlength with min and max to prevent wrong submits as stated above several times.

    0 讨论(0)
  • 2020-11-22 05:26

    If you are looking for a Mobile Web solution in which you wish your user to see a number pad rather than a full text keyboard. Use type="tel". It will work with maxlength which saves you from creating extra javascript.

    Max and Min will still allow the user to Type in numbers in excess of max and min, which is not optimal.

    0 讨论(0)
  • 2020-11-22 05:26

    As stated by others, min/max is not the same as maxlength because people could still enter a float that would be larger than the maximum string length that you intended. To truly emulate the maxlength attribute, you can do something like this in a pinch (this is equivalent to maxlength="16"):

    <input type="number" oninput="if(value.length>16)value=value.slice(0,16)">
    
    0 讨论(0)
  • 2020-11-22 05:27

    You can try this as well for numeric input with length restriction

    <input type="tel" maxlength="3" />
    
    0 讨论(0)
提交回复
热议问题