views:

16

answers:

2

I'm using a CMS to allow users to add keywords to their profiles, but I'm wanting to limit that to 10 keywords, which are comma delimited, by counting the number of commas (in this case, 9).

Any ideas on how to go about doing this with jQuery? I'll be doing a double-check on the server-side, but I'd like a quick little, live error checker on the front-end.

Thanks for any help!

+1  A: 

instead of counting commas, how about splitting them then get the length.

var count = 'reigel,me,you,we'.split(',').length; // results 4

fiddle to see.

and if you're using this in a <form>, you could edit the submit handler like this,

$('#formID').submit(function(){
    if ($('#inputTextId').val().split(',').length < 10) {
        return false; // prevent page from submitting...
    }
})​;​

another fiddle

Welcome to stackoverflow.com
Don't forget to accept an answer

Reigel
Awesome answer. I've implemented something similar to your second suggestion and it works great. Thanks so much for your help!
Jordan