For <input type="number">
element, maxlength
is not working. How can I restrict the maxlength
for that number element?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
-
Why does the box-shadow property not apply to a
it's very simple, with some javascript you can simulate a
maxlength
, check it out: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.
And you can add a
max
attribute that will specify the highest possible number that you may insertif you add both a
max
and amin
value you can specify the range of allowed values:The above will still not stop a user from manually entering a value outside of the specified range. Instead he will be displayed a popup telling him to enter a value within this range upon submitting the form as shown in this screenshot:
You can combine all of these like this:
Update:
You might also want to prevent any non-numeric characters to be entered, because
object.length
would be an empty string for the number inputs, and therefore its length would be0
. Thus themaxLengthCheck
function won't work.Solution:
See this or this for examples.
Demo - See the full version of the code here:
http://jsfiddle.net/DRSDavidSoft/zb4ft1qq/1/
Update 2: Here's the update code: https://jsfiddle.net/DRSDavidSoft/zb4ft1qq/2/
Update 3: Please note that allowing more than a decimal point to be entered can mess up with the numeral value.
I know there's an answer already, but if you want your input to behave exactly like the
maxlength
attribute or as close as you can, use the following code:Maycow Moura's answer was a good start. However, his solution means that when you enter the second digit all editing of the field stops. So you cannot change values or delete any characters.
The following code stops at 2, but allows editing to continue;