views:

21

answers:

1

I am developing a website which is similar to stackoverflow.com. A poster is required to enter 1 to 3 tags for his problem or task.If a tag has more than one word, then - is used to 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.

   $.validator.addMethod("tagcheck", function(value, element) { 
 return //To determine the value contains no more than 3 words.

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

How to write the customized method?

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

}, "Please input at most 3 tags.");
apphacker
What if there are two adjacent spaces?
Steven
Maybe a word counter is better? What if a user enters more than once space at once? For example, tag1 tag2, there are more than one space between tag1 and tag2.
Steven
consider it a guide, improve to suit your needs. Unless you want to pay me
apphacker
Haha, good. I am developing such a website that an asker can pay a solver.
Steven
@apphacker, thank you anyway. With the help of your guide, I write my own statement:return value ]*/).length < 4;
Steven
Oh, my statement is wrong.
Steven