PhpxZ Posted July 8, 2010 Share Posted July 8, 2010 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> Quote Link to comment Share on other sites More sharing options...
haku Posted July 8, 2010 Share Posted July 8, 2010 You can't do that purely through HTML. You could do it with javascript, but anyone who turns it off would be able to bypass it. Quote Link to comment Share on other sites More sharing options...
PhpxZ Posted July 8, 2010 Author Share Posted July 8, 2010 and how can i do it with javascript? Quote Link to comment Share on other sites More sharing options...
PhpxZ Posted July 8, 2010 Author Share Posted July 8, 2010 and how can i do it with javascript? Quote Link to comment Share on other sites More sharing options...
PhpxZ Posted July 9, 2010 Author Share Posted July 9, 2010 Anyone? :-\ Quote Link to comment Share on other sites More sharing options...
radar Posted July 12, 2010 Share Posted July 12, 2010 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.