如何获取Jquery表单验证插件中的单词数? [英] How to get the number of words in Jquery form validation plugin?

查看:114
本文介绍了如何获取Jquery表单验证插件中的单词数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是标签输入框验证.张贴者可以将多个单词组合成单个单词,并使用空格分隔标签.我使用Jquery表单验证插件来验证表单.我需要添加自定义方法来验证标签输入框. 这是代码:

It is tag input box validation.A poster can combine multiple words into single-words, space is used to separate tags. I use Jquery form validation plugin to validate the form. I need to add a customized method to validate the tag input box. This is the code:

 $.validator.addMethod("tagcheck", function(value, element) { 
     return value && value.split(" ").length < 4;

    }, "Please input at most 3 tags.");

但是如果两个相邻单词之间有两个空格怎么办?

But what if there are two spaces between two adjacent words?

推荐答案

您可以使用正则表达式来

You can use a regular expression to split your string:

$.validator.addMethod("tagcheck", function(value, element) { 
  return value && value.split(/\s+/).length < 4;
}, "Please input at most 3 tags.");

\s+将匹配一个或多个空格字符,包括空格,制表符,换页符,换行符.

\s+ Will match one or more white space characters, including space, tab, form feed, line feed.

这篇关于如何获取Jquery表单验证插件中的单词数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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