tags:

views:

3160

answers:

3

the text input only allows three words separated by spaces, and if it exceeds 3, the user can't input anymore, is this possible using jQuery? I can use keyup event to listen, but how do I stop user from typing in more tokens without using disabled. This is sort of similar to the native maxlength property for the text input in html except that the maxLength in this case is the number of tokens.

+3  A: 

EDIT: Something like this should do it (tested):

    $('#myTextbox').keyup(function(e){
        var wordArr = $(this).val().split(' ');
        if(wordArr.length > 3 && e.keyCode == 32) {
            var arrSlice = wordArr.slice(0,3);
            var newStr = arrSlice.join(' ');
            $(this).val(newStr);
        }
    });

If the user attempts to type in more than three words, the contents will be overwritten with only the first three words. This solution will show characters being removed as they are typed if three words are exceeded, thus providing a bit of feedback.

If that's not the desired behaviour, @Jataro's solution is the way to go.

karim79
Think he needs to allow entire words
Damien
Will this stop someone from right clicking and pasting a large amount of text into the field?
marcc
Oops, just saw that. Editing...
karim79
He said he wants to allow three **words**, not three characters.
SolutionYogi
@marcc - no, he would need to bind to the paste event to handle that
karim79
Edited, methinks that should solve the problem.
karim79
I would suggest that you keep the original array from the split instead of recreating again in the 'if' condition. The reason is that this code will be called from keyup event and you want your code want to do as little as possible.
SolutionYogi
@SolutionYogi - that is true, optimizifying as we speak
karim79
The paste event is not cross browser, is there a cross browser paste solution?
A: 

May be something like,

$('#myTextbox').change(

    function()
    {
         var input = $(this).val();
         var numberOfWords = input.split(' ').length;
         if(numberOfWords > 3)
         {
              $(this).val(input.substring(0, input.lastIndexOf(' ')-1));
         }
    }
);

I haven't verified this code, but something like this should work.

And yes, as mgroves said, don't forget to re-validate everything on the server.

SolutionYogi
+4  A: 

Check if the input already has 3 words and they are trying to enter a space. If so return false:

$('#myTextbox').keydown(
    function(event)
    {
        var input = $(this).val();
        var numberOfWords = input.split(' ').length;
        if(numberOfWords == 3 && event.keyCode == 32)
        {
            return false;
        }
    }
);

Edit: There was an additional question about the situation where someone might past text in the field. As a cross browser solution I would probably bind code similar to @karim79's to the blur event.

$('#myTextbox').blur(
    function(e)
    {
        var tokens = $(this).val().split(' ');
        if(tokens.length > 3) 
        {
            $(this).val(tokens.slice(0,3).join(' '));
        }
    }
);
Jataro
+1 Solid solution
karim79
very good. as someone mentioned above, is there a cross browser way to handle the situation where user do a copy and paste into the input? the paste event is not cross-browser.
blur event doesn't really work for detecting paste since the text input is in focus before past, and still in focus after paste..any other idea?
I am unaware of a cross-browser way to get the paste event. The solution I provided will at least ensure that the string is truncated before submit. See http://stackoverflow.com/questions/237254 for more on JavaScript paste events.
Jataro
That's what you call 'thinking outside the box'. Fantastic!
SolutionYogi