Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the maxlength of an input field using javascript

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);
like image 674
Tech Tech Avatar asked Oct 01 '13 15:10

Tech Tech


People also ask

Does input tag attribute 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.

How can I get maximum length of textbox?

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 you set the length of an input number in HTML?

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.


2 Answers

document.getElementById("password").maxLength

To access it with Javascript you need an uppercase 'L'

W3 Schools Example

like image 61
Tyler Avatar answered Sep 27 '22 17:09

Tyler


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'));
like image 44
BlitZ Avatar answered Sep 27 '22 18:09

BlitZ