Javascript DOM HTML Input Text maxLength Property

Introduction

Get the maximum number of characters allowed in a specific text field:

var x = document.getElementById("myText").maxLength;

Click the button to display the value of the maxlength attribute in the text field.

View in separate window

<!DOCTYPE html>
<html>
<body>

Name: <input type="text" id="myText" maxlength="30">
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from ww w.j  a v  a  2 s  . co m*/
  var x = document.getElementById("myText").maxLength;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

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

The maxLength attribute sets the maximum number of characters allowed in a text field.




PreviousNext

Related