Jump to content

How can i limit maximum chars per row?


PhpxZ

Recommended Posts

How to Limit number of character in each line in textarea Let say script should allow maximum 4 rows and should allow 8 character in each line. Also If character reached the limit then it should move to next line.

 

its will look like this:

 

12345678

90123456

78901234

 

 

this is my code what i need to add

<textarea name="text" cols="76" rows="15" lang="lt"></textarea>

Link to comment
https://forums.phpfreaks.com/topic/207121-how-can-i-limit-maximum-chars-per-row/
Share on other sites

You could do something like this:

 

<textarea cols="20" rows="5" id="t1" onkeyup="testchar();"></textarea>

<script type="text/javascript">
<!--
function testchar(){
var lastpos = 0;
var numlines = 1;
var lineend = 20;
var linelength = 20;

var my_string = document.getElementById("t1").value
for(i=0;i<my_string.length;i++){

if(i>lineend){
numlines= parseInt(numlines) + parseInt('1');
lineend= parseInt(lastpos) + parseInt(linelength) + parseInt('1');
lastpos=i;

}
if(my_string.charAt(i)==' '){
lastpos = i;
//alert('blank found at' + my_string.charAt(i))
}
var myRegExp = /\n/
if (myRegExp.test(my_string.charAt(i))) {
alert('hello')
numlines= parseInt(numlines) + parseInt('1');
lineend= parseInt(lineend) + parseInt(linelength) + parseInt('1');
lastpos=parseInt(lineend) - parseInt(linelength);

}

}

if(numlines > 5){
alert('You are beyond the number of lines allowed, please revist your text');
// alert('number of lines' + numlines);
// alert('last position of a space' + lastpos);
// alert('current line end value' + lineend);
// alert(i);
}
}
//-->
</script>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.