views:

48

answers:

3

How to check if certain row in textarea is empty with javascript?

<script type="text/javascript" src="../../../js/jquery-1.3.2.min.js"></script>
<script>
$(document).bind('keydown',function (e) {

if(e.which==13) {
function isRowEmpty( idOfTextArea , row ){

  return $("#" + idOfTextArea )[0].value.split("\n")[row-1] == "";

}
alert(isRowEmpty("txtarr",3));

    }
});
</script>
<textarea id="txtarr" rows="30" ></textarea>

But it returns always false

+1  A: 

use string.split("\n", limit), where string is the textarea value.

Then, use your standard FOR loop, and do something like if (list[i].match("/(\s)/")) to check for a line that is only whitespace.

If I want to check whether the last row is empty?
lam3r4370
If for certain row, the i should be predefined.
ZEAXIF
According to the example, for last row:var list = string.split(separator, limit);if(list[list.length-1] == ""){// to do...} should do the trick
ZEAXIF
What to write on the seprator ,limit place?seprator - / ? But limit?
lam3r4370
Sorry, I forgot to mention that lines are separated by "\n"
A: 
<script type="text/javascript">
<!--
function rows() {
var lines;
var TA=document.form1.textarea1.value;
if(document.all) { // IE
lines=TA.split("\r\n");
}
else { //Mozilla
lines=TA.split("\n");
}
for(var i=0; i<lines.length; i++) {
    if (lines[i].replace(/^\s+|\s+$/, '').length == 0) {
        alert('Line ' + i + ' is empty')
    }
}
}
//-->
</script>
smink
A: 
//Assumes you use jQuery or equivalent
function isRowEmpty( idOfTextArea , row ){

  return $("#" + idOfTextArea )[0].value.split("\n")[row-1] == "";

}
tomdemuyt
<script type="text/javascript" src="../../../js/jquery-1.3.2.min.js"></script><script>$(document).bind('keydown',function (e) {if(e.which==13) {function isRowEmpty( idOfTextArea , row ){ return $("#" + idOfTextArea )[0].value.split("\n")[row-1] == "";}alert(isRowEmpty("txtarr",3)); }});</script><textarea id="txtarr" rows="30" ></textarea>But it returns always false?
lam3r4370
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <TITLE> New Document </TITLE> <script type="text/javascript" charset="utf-8" src="lib/jquery-1.4.2.js"></script> </HEAD> <BODY> <textarea id="txtarr" rows="30" ></textarea><script> $(document).bind('keydown',function (e) { if(e.which==13) { function isRowEmpty( idOfTextArea , row ){ return $("#" + idOfTextArea )[0].value.split("\n")[row-1] == ""; } alert(isRowEmpty("txtarr",3)); } }); </script> </BODY></HTML>Works for me.
tomdemuyt
.toString(); gives me ---fffffffffffffffffsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
lam3r4370