Reputation: 212
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
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
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');
}
});
Upvotes: 1