Jim SMith
Jim SMith

Reputation: 212

jquery check for long strings in a textarea

I have users typing in incredibly long strings, as below, is there a way in jquery to detect that a string is getting too long so that I can warn the user?

<textarea>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</textarea>

I'm thinking a keyup that checks the length since the last space (or since the beginning if no spaces)

Upvotes: 0

Views: 148

Answers (2)

Nick.T
Nick.T

Reputation: 577

You can do something like this :

<textarea id="myTA" maxlength="1000"></textarea>

then with jQuery :

function(){
    $("myTA").change(function(_itm){
        if(_itm.length >= 1000)
            alert("Too long");
        });
}();

You should get something with this, try it out...

Upvotes: 0

Gurpreet Singh
Gurpreet Singh

Reputation: 21233

Try this:

It checks the string length of word since last space.

$('#test').keyup(function() {
    var string = $(this).val();
    var n = $(this).val().lastIndexOf(" ");

    var lastword= string.substring(n+1, string.length);

    if(lastword.length > 10) {
      alert('Too long');        
    }     
});​

DEMO

Upvotes: 1

Related Questions