开发者

Setting a minimum length for textarea

开发者 https://www.devze.com 2023-01-26 02:49 出处:网络
I am trying to make it so that this comment form on my wordpress website can not be submitted if the user does not enter enough data into the textarea.

I am trying to make it so that this comment form on my wordpress website can not be submitted if the user does not enter enough data into the textarea.

I wrote this for the header

<script>
function CheckLength()
{
var msg_area = document.getElementById("Message");
msg_area.innerHTML = "";
if (document.getElementById("commentarea").value.length < 100) {
msg_area.innerHTML = "YOU DID NOT ENTER ENOUGH INFO FOR YOUR REVIEW";
}
else document.getElementById("commentform").submit();
}
</script>

And my form looks like this

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" name="commentform" id="commentform">


           <p><input size="36" type="text" name="author" /> Name <span class="required">*</span></p>
            <p><input size="36" type="text" name="email" /> Email <span class="required">*</span> (Not Published)</p>
            <p><input size="36" type=开发者_StackOverflow中文版"text" name="url" /> Website</p>
            <p><input size="36" type="text" name="server_ip" id="server_ip" /> Server IP/Hostname</p>
            <p><?php show_subscription_checkbox(); ?></p>     

            <p><textarea name="comment" id="commentarea" cols="100%" rows="20"></textarea></p>

        <p align="right"><input type="button" name="submit" id="submit" value="Submit Review" tabindex="5" onClick="CheckLength()"></p>
        <span id="Message" style="color:#ff0000"></span>


    <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />

    <?php /* do_action('comment_form', $post->ID); */ ?>
        </form>

The form spits out the error "YOU DID NOT ENTER ENOUGH INFO FOR YOUR REVIEW" if I type in less then 100 chars, but if I type more then a 100 chars the form does not submit. If I change the input type to submit it does submit, but it doesn't matter if < 100


Change the name of your input button.

The error I get using your markup is that submit is not a function, because it thinks I'm referring to the submit button. The following should work:

<input type="button" name="submitbtn" id="submitbtn" value="Submit Review" tabindex="5" onClick="CheckLength()">


Could you do this?

Put the check in the "onsubmit" event for the form:

<form action="wp-comments-post.php" onsubmit="return CheckLength();" method="post" name="commentform" id="commentform">

You'll have to update you function to return a Boolean:

<script type="text/javascript">

function CheckLength()
{
    var msg_area = document.getElementById("Message");
    msg_area.innerHTML = "";
    if (document.getElementById("commentarea").value.length < 100) {
        msg_area.innerHTML = "YOU DID NOT ENTER ENOUGH INFO FOR YOUR REVIEW";
        return false;
    }
    return true;
}
</script>

Then remove the onclick event from your submit button.


Can you try this?

<script>
function CheckLength()
{
    var msg_area = document.getElementById("Message");
    msg_area.innerHTML = "";
    if (document.getElementById("commentarea").value.length < 100)
    {
        msg_area.innerHTML = "YOU DID NOT ENTER ENOUGH INFO FOR YOUR REVIEW";
        return false;
    }
    else
    {
        document.getElementById("commentform").submit();
        return true;
    }
}
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消