Ste*_*her 3 javascript jquery jquery-validate
我正在寻找一种方法来使用jQuery验证一些输入textareas - 不幸的是,因为jQuery验证器插件很棒,它缺乏验证(据我所知)"最低要求的单词".我没有代码(我早上会编辑),但是我编写了一个函数来计算输入中的单词,但这不是像插件提供的那样干净的验证.
我也查看了word-and-character-count.js,但是为了提交表单,这也没有提供最小字数.
编辑:我提供的答案是一个自定义验证方法 - 如果有人知道任何更清洁的方法,甚至一个易于使用的插件,请告诉我.
Ste*_*her 11
获取实时字数的功能,不包括末尾的空格(简单的分割将计数说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']
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5629 次 |
| 最近记录: |