Validate the form before viewing like this, and use the strlen parameter to check the input length:
if(isset($_POST['mySubmit'])) { if(strlen($_POST['try']) < 5) { $error = "Too short"; } else { $valid = true; //Do whathever you need when form is valid } } else { if(isset($error)) { echo "<p>$error</p>"; } //echo your form here echo "<form method='post' action='thisPhpScript.php'> <input type='text' name='try' size='10' id='try' maxlength='5' > </form>"; }
Not tested, so they may have syntax errors.
source share