Javascript DOM HTML Input Text maxLength Property set

Introduction

Set the maximum number of characters allowed in a text field:

document.getElementById("myText").maxLength = "4";

Click the button to set the maximum number of characters allowed in the text field.

View in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {/*from w w  w. j  a  va  2s.  c o m*/
  document.getElementById("myText").maxLength = "4";
  document.getElementById("demo").innerHTML = "Maximum number of characters allowed in the text field is now 4.";
}
</script>

</body>
</html>



PreviousNext

Related