Skip to content
Advertisement

How to autosubmit a form in JS when a field reaches a number of characters? [closed]

What better way to make a form having a autosubmit when a field is completely filled? I need him to do it when it reaches 7 characters in the field.

Advertisement

Answer

<html>
<head>
<script type="text/javascript">//<![CDATA[
window.onload=function() {
    document.getElementById('myField').oninput=function() {
        if (this.value.length >= 7) {
            document.getElementById('myForm').submit();
        }
    };
};
//]]>
</script>
</head>
<body>
<form id="myForm" method="post" action="thispage.php">
<input id="myField" name="myField" type="text" />
</form>
</body>
</html>

would probably do the trick. However, what if the user makes a mistake on the 7th character? It seems a bit user-unfriendly to automatically submit the form like this.

Advertisement