Javascript Reference - HTML DOM Input Password maxLength Property








The maxLength property sets or gets the maxlength attribute of a password field.

The maxlength attribute sets the maximum number of characters allowed in a password field.

Browser Support

maxLength Yes Yes Yes Yes Yes

Syntax

Return the maxLength property.

var v = passwordObject.maxLength 

Set the maxLength property.

passwordObject.maxLength=integer 




Property Values

Value Description
integer Set the maximum number of characters allowed in the password field

Return Value

A Number type value representing the maximum number of characters allowed in the password field.

Example

The following code shows how to set the maximum number of characters allowed in a password field.


<!DOCTYPE html>
<html>
<body>
<!--   w ww.ja  va 2s  . c  o  m-->
Password: <input type="password" id="myPsw">
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myPsw").maxLength = "10";
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the maximum number of characters allowed in a specific password field.


<!DOCTYPE html>
<html>
<body>
<!--from  w  w w. j av a2  s. c  o  m-->
Password: <input type="password" id="myPsw" maxlength="8">
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myPsw").maxLength;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: