jQuery Validator最小必需单词

我正在寻找一种方法来使用jQueryvalidation一些输入textareas – 不幸的是,因为jQueryvalidation器插件很棒,它缺乏validation(据我所知)“最低要求的单词”。 我没有代码(我早上会编辑),但是我编写了一个函数来计算输入中的单词,但这不是像插件提供的那样干净的validation。

我也查看了word-and-character-count.js,但是为了提交表单,这也没有提供最小字数。

编辑:我提供的答案是一个自定义validation方法 – 如果有人知道任何更清洁的方法或甚至一个易于使用的插件,请告诉我。

获取实时字数的function,不包括末尾的空格(简单地分割将计算word (注意末尾的空格)作为2个单词。

 function getWordCount(wordString) { var words = wordString.split(" "); words = words.filter(function(words) { return words.length > 0 }).length; return words; } //add the custom validation method jQuery.validator.addMethod("wordCount", function(value, element, params) { var count = getWordCount(value); if(count >= params[0]) { return true; } }, jQuery.validator.format("A minimum of {0} words is required here.") ); //call the validator selector: { required: true, wordCount: ['30'] }