42

I am little bit confused about the difference between the maxlength and the size attribute.

<input type="text" name="telephone" maxlength="30" size="34">

I know that maxlength is used to control the input data. So what is the reason for using both attributes at once?

psantiago
  • 695
  • 6
  • 16
Hashan
  • 656
  • 1
  • 8
  • 20
  • 2
    The `size` attribute is used when calculating the display size of the control. `max-length` limits the amount of characters a user can enter (but it still has to be validated on the server) – knittl Aug 11 '14 at 16:07
  • 1
    @knittl, The `maxlength` attribute is not hyphenated. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input – Sparky Aug 11 '14 at 16:34
  • 1
    @Sparky: good catch. Unfortunately I cannot edit my comment anymore. – knittl Aug 11 '14 at 17:05

5 Answers5

48

The maxlength (not max-length) attribute specifies the maximum length of the input string in characters or, more exactly, in code units. The browser is expected to enforce this, by refusing to accept more characters. However, this is not meant to act as a security measure, since it can be overridden trivially. Rather, it tells the user that no more characters will be accepted in processing the data. This is useful when you have to set an upper limit, e.g. your database can store only a fixed number of characters fpr some information, and also when there is a logical limit on the length (e.g., if the data is a two-letter code for a US state, it has the logical upper limit of 2).

The maxlength attribute is thus logical, and it is expected to work even in non-visual user interface. It is not meant to affect the visual appearance of the input field in any way.

The size attribute, in contrast, is for visual rendering only. It suggests a visible width for the field, in terms of “average” characters. This vague concept has not been clarified in specifications, and browsers implement it inconsistently. It works best when a monospace font is used. This attribute does not limit the amount of characters entered, but it affects usability: it is difficult to enter, say, a 30 characters long string in a field that lets you see only 10 characters at a time. The width of a field is also a signal to the user: it indicates the expected maximum width of the input.

It is often suitable to use both attributes, often with the same value. For example, if the field is for a 5-digit postal code, size=5 maxlength=5 is suitable, especially if you also set font-family: monospace, so that the actual width is more or less exactly five digits.

However, the values may differ. E.g., when asking for a line in a postal address, you might set size=30, since this is normally sufficient for a line, but maxlength=80, if this corresponds to the limitations set by your database or data processing and you have no particular reason not to allow such long lines.

The size attribute can in principle be replaced by CSS, since it deals with visual rendering only. However, the width is usually best set in characters, and there is no universally supported unit for the average width of a character in CSS; the new ch unit comes close, but isn’t quite the same and isn’t supported by old browsers.

Brhaka
  • 1,622
  • 3
  • 11
  • 31
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Why is width _"usually best set in characters"_? Is there a good technical reason why the CSS `width` of an `input` shouldn't be set in pixels? – Sparky Aug 11 '14 at 17:11
  • @Sparky, for usability, the width should reflect the expected maximum width for “normal” input, and this can usually best be formulated as a number of characters. Pixels would be a particularly problematic choice, since the widths of letters vary by letter, by font family, and by font size. – Jukka K. Korpela Aug 11 '14 at 17:45
7

maxlength is used to indicate the number of characters that can be entered in the input field regardless of how large the fields appears on the screen. The size attribute determines how wide the field will be on the screen.

E.g., I generally use this for things like entering an email address where it can be fairly long, but for aesthetic reasons (good web design) you want the input field to be a certain length.

<input type="text" name="email" maxlength="50" size="20">

Once a user put in more than 20 characters for their email address, the field begins to scroll as they type.

VoronoiPotato
  • 3,113
  • 20
  • 30
  • 1
    How is it good web design to prevent the user from seeing the entire address he is typing? Many email addresses are longer than 20 characters. – Jukka K. Korpela Aug 11 '14 at 16:34
  • @JukkaK.Korpela, I think in this case, "good design" was _only_ referring to conformance to a particular layout (_"for aesthetic reasons"_). This part is a matter of opinion, but in fairness to the poster, his solution 100% correctly answered the question (first paragraph), so I don't think a down-vote is justified, IMO. – Sparky Aug 11 '14 at 16:40
  • Well, the attribute name is not `max-length`, and an email address is not a good example, especially since it is probably better read using ``. – Jukka K. Korpela Aug 11 '14 at 16:54
  • @JukkaK.Korpela, yes, I agree, which is why I did not vote it up either. – Sparky Aug 11 '14 at 17:12
0

Max length refers to the maximum possible length of the input string. Size refers to the size of the input itself.

The Head Rush
  • 3,157
  • 2
  • 25
  • 45
0

size attribute can be used to change the size of the input field(How wide the field on the screen) where as maxlength attribute can be used to define a maximum number of characters for the input field.

Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
-1

size is "how big" it should be on screen. maxlength is the maximum number of characters the browser should allow to be entered into a field. They're not at all related. You could have have size = 50 kajillion, max-length=1 if you wanted to be a sadistic page designer.

VoronoiPotato
  • 3,113
  • 20
  • 30
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • The `maxlength` attribute is not hyphenated. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input – Sparky Aug 11 '14 at 16:34