<!DOCTYPE html>
<html>
<body>

Name: <input type="text" id="myText">

<p>Click the button to set the maximum number of characters allowed in the text field.</p>

<button onclick="myFunction()">Try it</button>

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

<script>
function myFunction() {
    document.getElementById("myText").maxLength = "4";
    document.getElementById("demo").innerHTML = "Maximum number of characters allowed in the text field is now 4.";
}
</script>

</body>
</html>