0

I am using HTML maxlength attribute in my mobile application but it is not working. I tried several other ways to limit the maximum length in input[type=text] but none of them is working as good as maxlength works in web browsers. So I want to know the coding logic behind maxlength attribute.

  • Please post your actual code. – Russ J Sep 27 '19 at 15:19
  • 2
    Can you show an example of the html where `maxlength` is not being enforced, and indicate the operating system, browser, and browser version where this is the case? Maybe a small modification to that might make it work. – Lonnie Best Sep 27 '19 at 15:19
  • I am working on Google chrome, version 77.0.3865.90 on Windows 10 OS and I am using inspect tools to check behavior in mobile. @LonnieBest – Tejas Kothari Sep 27 '19 at 15:31
  • My apologies for not able to post full code due to restriction by organization I work for. But I am testing by putting maxlength attribute in input tag. Also I am not trying to enforce any javascript code to do the same job when I am trying with maxlength attribute. – Tejas Kothari Sep 27 '19 at 15:34
  • meanwhile same kind of issue is already here on stack overflow : https://stackoverflow.com/questions/17439487/enforcing-the-maxlength-attribute-on-mobile-browsers – Tejas Kothari Sep 27 '19 at 15:39
  • If you click on the source-link, I provided in my answer, it shows some examples. Compare your use of `maxlength` carefully with those examples and see if there's anything different. This is my best suggestion without seeing your code. – Lonnie Best Sep 27 '19 at 15:39

2 Answers2

0

I understand that you're only interested in understanding how maxlength is supposed to work. Here's a quote from the specification:

4.10.5.3.1. The maxlength and minlength attributes

The maxlength attribute, when it applies, is a form control maxlength attribute controlled by the input element’s dirty value flag.

The minlength attribute, when it applies, is a form control minlength attribute controlled by the input element’s dirty value flag.

If the input element has a maximum allowed value length, then the code-unit length of the value of the element’s value attribute must be equal to or less than the element’s maximum allowed value length.

Source: §4.10.5.3.1

If a browser you're testing isn't complying with spec, submit a bug report to that browser. In the meantime, you could use javascript to enforce a maximum length.

However, even if you do enforce this on the client-side, you still need to enforce it server-side too, because it is easy to hack client-side code and circumvent all intended limitations before sending to the server.

Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
  • thanks for sharing the reference but it is not explaining that how maxlength stops the user from entering the text and comparing the string length with maxlength, is there any doc which explains that. Thanks – Tejas Kothari Sep 27 '19 at 16:07
  • this is exactly how i am using maxlength attribute in my application. – Tejas Kothari Sep 27 '19 at 16:21
  • This is all I know. Did you click on the source link underneath the quote? The specification has clickable hyperlinks to drill down into each term that has a detailed meaning. For example, go [here](https://www.w3.org/TR/html53/sec-forms.html#the-maxlength-and-minlength-attributes), then click on "form control maxlength attribute" and it shows [more details](https://www.w3.org/TR/html53/sec-forms.html#form-control-maxlength-attribute) about the logic. – Lonnie Best Sep 27 '19 at 17:29
0

Quoting MDN here:

The input will fail constraint validation if the length of the text value of the field is greater than maxlength UTF-16 code units long. Constraint validation is only applied when the value is changed by the user.

I see you tagged JavaScript...So, your problem is likely that maxlength doesn't prevent programmatic changes from exceeding that number of characters. For example:

document.querySelector('input').value = 'testing'
<input type="text" maxlength="2" />

...So, you will likely need to manually handle maxlength in your code. Perhaps something like this:

// NOTE: Truncates based on maxlength
const setTextInputValue = (el, str) => {
  const maxlength = +el.getAttribute('maxlength')
  el.value = str.substring(0, maxlength)
}

const el = document.querySelector('input')
setTextInputValue(el, 'testing')
<input type="text" maxlength="2" />
BCDeWitt
  • 4,540
  • 2
  • 21
  • 34
  • Yeah this is the one way but what we are doing is taking the sub-string of maximum allowed length. The problem is when a user enters a text anywhere in between. Now sub-string function will remove the characters from the end but what should have happened is user must not be allowed to enter (done by the maxlength attribute) – Tejas Kothari Sep 27 '19 at 16:16
  • I'm not sure I understand. Are you using the input from one textbox to programmatically set another? – BCDeWitt Sep 27 '19 at 16:21
  • No, for now I am just typing values in input field and checking if it stops user from entering the text in the input field. – Tejas Kothari Sep 27 '19 at 16:23