I have a text area where each line contains Integer value like follows
1234
4321
123445
I want to check if the user has re
This works without needing jQuery:
var textArea = document.getElementById("my-text-area");
var arrayOfLines = textArea.value.split("\n"); // arrayOfLines is array where every element is string of one line
Try this.
var lines = $('textarea').val().split('\n');
for(var i = 0;i < lines.length;i++){
//code here using lines[i] which will give you each line
}
A simple regex should be efficent to check your textarea:
/\s*\d+\s*\n/g.test(text) ? "OK" : "KO"
var textArea = document.getElementById('myTextAreaId');
var lines = textArea.value.split('\n'); // lines is an array of strings
// Loop through all lines
for (var j = 0; j < lines.length; j++) {
console.log('Line ' + j + ' is ' + lines[j])
}
var lines = $('#myTextAreaId').val().split('\n'); // lines is an array of strings
// Loop through all lines
for (var j = 0; j < lines.length; j++) {
console.log('Line ' + j + ' is ' + lines[j])
}
Side note, if you prefer forEach a sample loop is
lines.forEach(function(line) {
console.log('Line is ' + line)
})
This would give you all valid numeric values in lines
. You can change the loop to validate, strip out invalid characters, etc - whichever you want.
var lines = [];
$('#my_textarea_selector').val().split("\n").each(function ()
{
if (parseInt($(this) != 'NaN')
lines[] = parseInt($(this));
}