views:

137

answers:

1

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?

+2  A: 

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.

CMS
ye `CMS` is the new Jon Skeet ;)
Rakesh Juyal