문제

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!

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top