I just wanted to know if we could get the maxlength of an input field from javascript
<input type="password" id="password" maxlength="20" >
I tried this but it returns undefined
console.log(document.getElementById("password").maxlength);
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.
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.
Input value length You can specify a minimum length (in characters) for the entered value using the minlength attribute; similarly, use maxlength to set the maximum length of the entered value, in characters. The example below requires that the entered value be 4–8 characters in length.
document.getElementById("password").maxLength
To access it with Javascript you need an uppercase 'L'
W3 Schools Example
Use DOMElement::getAttribute()
to obtain properties, that not listed in DOMElement, but existing in markup:
var el = document.getElementById("password");
console.log(el.getAttribute('maxlength'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With