Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML maxlength attribute not working on chrome and safari?

Tags:

html

<input type="number" maxlength="5" class="search-form-input" name="techforge_apartmentbundle_searchformtype[radius]" id="techforge_apartmentbundle_searchformtype_radius"> 

This is my HTML, taken with firebug (on chrome).

I am allowed to write as much as characters as I want in the form field - in Chrome and Safari.

When on Firefox or IE10, the limit is fine.

I haven't found this issue around on the net.

Note: type="number" - not text.

Anyone saw this issue before?

like image 413
Tool Avatar asked Mar 04 '12 13:03

Tool


People also ask

Why is input Maxlength not working?

If your input is of type "number", the max length will not work. You can use javascript to try to limit, or you can use a Text Input instead, attached to a number variable, if you do not need the spin buttons.

Does Maxlength work all browsers?

All browsers support maxlength. However, this attribute can easily be removed/changed using DOM methods or, for example, with Firefox's Web Developer Toolbar.

How do I set input Maxlength in HTML?

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.


2 Answers

Max length will not work with <input type="number" the best way i know is to use oninput event to limit the maxlength. Please see the below code.

<input name="somename"          oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"          type = "number"          maxlength = "6"       />
like image 129
Neha Jain Avatar answered Sep 23 '22 21:09

Neha Jain


Use the max attribute for inputs of type="number". It will specify the highest possible number that you may insert

  <input type="number" max="999" /> 

if you add both a max and a min value you can specify the range of allowed values:

  <input type="number" min="1" max="999" /> 

See this example

EDIT

If, for user experience, you would prefer the user not to be able to enter more than a certain number, use Javascript/jQuery, as seen in this example

like image 36
jacktheripper Avatar answered Sep 19 '22 21:09

jacktheripper