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.
maxLength |
Yes | Yes | Yes | Yes | Yes |
Return the maxLength property.
var v = passwordObject.maxLength
Set the maxLength property.
passwordObject.maxLength=integer
Value | Description |
---|---|
integer | Set the maximum number of characters allowed in the password field |
A Number type value representing the maximum number of characters allowed in the password field.
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:
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: