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
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>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.