jQuery验证字数集成 [英] jquery validation word count integration

查看:156
本文介绍了jQuery验证字数集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字数集成到bassistance.de验证中?例如,我不希望人们输入50个以上的单词,并且应该显示一个错误来表示它.

How can I integrate word count into bassistance.de validation? For example, i dont want people to enter more than 50 words, and it should show an error to indicate it.

<form action="done.php" method="post" id="mainform"  class="validate">
<textarea name="message" rows="4" id="message"  class="required" ></textarea>   
</form>

这是我的全部,但我不知道从这里去哪里

Here is all I have, but i dont know where to go from here

$(document).ready( function() {
$("form.validate").validate({
    submitHandler: function(form) {
               form.submit();
    }
})

});

在此提供任何帮助,我一点都不熟悉这个插件.

any assistance here would be appreciated, i'm not familiar with this plugin at all.

推荐答案

根据您认为的平均单词长度(我仍然落后5位),我会采用最少的字符数.这样就可以了:

I would go with a minimum charachter count, based on what you think is the average word length (I still stand behind 5). So that would be:

$("#mainform").validate({
  rules: {
    message: {
      required: true,
      minlength: 250
    }
  }
});

更新:

如果ajax是一个选项,则可以使用remote规则.我认为它将遵循以下原则:

Update:

If ajax is an option, you could use the remote rule. I think it would along the lines of:

$("#test").validate({
    rules: {
        message: {
            remote: {
                url: "check_wordcount.php",
                type: "post",
                data: {
                    wordcount: 5
                }
            }
        }
    }
}

我在时间安排上遇到了一些问题,但是我认为其中一些只是我匆忙完成的事情,但是它确实有效.在这种情况下,远程脚本必须返回true作为元素名称,因此我的php脚本如下所示:

I'm having some issues with it, with the timing, but I think some of that is just me rushing things, but it does work. In this case, the remote script must return true for the element name, so my php script looks like:

$message = $_POST['message'];
$wordcount = (int) $_POST['wordcount'];
$valid = (str_word_count($message) >= $wordcount);

echo json_encode(array('message' => $valid));

这篇关于jQuery验证字数集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆