I have been searching all weekend for the solution to this quandry and have yet to find a solution that works correctly. What I am trying to acchieve is to limit the number of c
Below is a sample snapshot of the problem that you're trying to solve:
Snapshot of the textarea:
123456789012345678901234
123456789012345678902333
232323232323232323323232
23232323232323232323236464536543654643
JavaScript:
$('#your-input').keypress(function() {
var text = $(this).val();
var arr = text.split("\n");
if(arr.length > 5) {
alert("You've exceeded the 4 line limit!");
event.preventDefault(); // prevent characters from appearing
} else {
for(var i = 0; i < arr.length; i++) {
if(arr[i].length > 24 && i < 3) {
alert("Length exceeded in line 1, 2, or 3!");
event.preventDefault(); // prevent characters from appearing
}
}
}
console.log(arr.length + " : " + JSON.stringify(arr));
});
This can be accomplished using a keypress event. When the keypress event fires, get the current value of the textbox and split the value into an array using the \n
line break character as a delimiter.
This is not designed to be a complete solution, but this will definitely get you started and provide you with something that you can modify to suit your needs. Good luck!