开发者

How to get the number of words in Jquery form validation plugin?

开发者 https://www.devze.com 2022-12-12 21:19 出处:网络
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 customiz

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 inpu开发者_StackOverflowt 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+ Will match one or more white space characters, including space, tab, form feed, line feed.

0

精彩评论

暂无评论...
验证码 换一张
取 消