wp_editor delay form validation
-
I’m trying to create a form which load content from the database ($content) and includes a wordpress editor (wp_editor function), and apply some characters count validation. For example:
<form id="something" action="example.php" name="something" method="post" onsubmit="return Check ()"> <?php wp_editor( $content, 'content', $settings = array( 'textarea_name' => 'content' ) ); ?> <input type="submit" value="Submit" name="submit" /> </form>
Here’s the javascript part to do the characters count validation:
<script type="text/javascript"> function Check () { var content = document.getElementById ("content"); //Minimum number of characters if (content.value.length < 400) { alert ("The content must be at least 400 characters long!"); return false; } //Maximum number of characters if (content.value.length > 2000) { alert ("The content must be at most 2000 characters long!"); return false; } return true; } </script>
The problem is, let say $content output 1000 characters initially. If I reduce it to 200 characters (less than the minimum) and press submit, the form still submit without displaying the alert nor returning false, bypassing the characters count.
Futhermore, if $content output 3000 characters initially (more than the maximum) and I reduce it to 1000 characters then press submit, the form will display the alert and return false instead of the opposite, and the form only submit if I press the submit input the second time.
It seems that loading wp_editor delay the validation process, as I don’t have this issue when using normal
<textarea>
Can someone explain why this is happening? And is there a solution or alternative for this issue? Thanks.
- The topic ‘wp_editor delay form validation’ is closed to new replies.