Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is <input type="number" maxlength="3"> not working in Safari?

I like to have an input with maximum 3 characters. In Firefox everything works fine I can only write 3 numbers inside the input - but in safari you can write a lot of numbers inside.

You can test it in both browsers with this: http://flowplayer.org/tools/demos/validator/custom-validators.htm

For now I realized it with a validate plugin - but just for interest I like to know why isn’t this working?

EDIT:

with this it's working

<input class="required" id="field" type="text" maxlength="3" pattern="([0-9]|[0-9]|[0-9])" name="cvv"/> 

answer: How can I prevent letters inside a text element?

like image 698
Jules Avatar asked Feb 20 '12 12:02

Jules


People also ask

How do you set the maxlength of input?

The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.

How do you set the maximum number of characters allowed in the input field to 40?

Complete HTML/CSS Course 2022 To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

How do I allow only input field numbers?

By default, HTML 5 input field has attribute type=”number” that is used to get input in numeric format. Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.


2 Answers

I've accomplished it with:

<input class="required" id="field" type="text" maxlength="3" pattern="([0-9]|[0-9]|[0-9])" name="cvv"/> 

and then in JavaScript I prevented the letters:

$("#myField").keyup(function() {     $("#myField").val(this.value.match(/[0-9]*/)); }); 
like image 92
Jules Avatar answered Sep 22 '22 10:09

Jules


You may try to user type="text" and oninput as below. This will let the numbers only.

<input type="text" maxlength="3" oninput="this.value=this.value.replace(/[^0-9]/g,'');" id="myId"/> 
like image 35
Viktor Karpyuk Avatar answered Sep 20 '22 10:09

Viktor Karpyuk