设置文本区域的最小长度

发布于 2024-10-03 08:33:21 字数 1841 浏览 6 评论 0原文

我正在尝试这样做,以便如果用户没有在文本区域中输入足够的数据,则无法提交我的 WordPress 网站上的评论表单。

我为标题写了这个

<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>

,我的表单看起来像这样

<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="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>

如果我输入的字符少于 100 个字符,则表单会输出错误“您没有为您的评论输入足够的信息”,但如果我输入的字符多于 100 个字符,则表单会显示错误不提交。如果我更改输入类型以提交,它确实会提交,但如果 << 也没关系。 100

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="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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清欢 2024-10-10 08:33:21

更改输入按钮的名称。

我使用您的标记得到的错误是提交不是一个函数,因为它认为我指的是提交按钮。以下应该有效:

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

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()">
情泪▽动烟 2024-10-10 08:33:21

你能做到这一点吗?

将检查放入表单的“onsubmit”事件中:

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

您必须更新函数以返回布尔值:

<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>

然后从提交按钮中删除 onclick 事件。

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.

緦唸λ蓇 2024-10-10 08:33:21

你能试试这个吗?

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文