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 https://forums.phpfreaks.com/topic/207123-how-to-limit-number-of-character-in-each-line-in-textarea/ Share on other sites More sharing options...
freelance84 Posted July 8, 2010 Share Posted July 8, 2010 I'm confused, what do you want to do? A user enters 8 characters into a text area and on the 9th it starts a new line? Is this not dependant on the size of your text area? Trial and error? Quote Link to comment https://forums.phpfreaks.com/topic/207123-how-to-limit-number-of-character-in-each-line-in-textarea/#findComment-1082979 Share on other sites More sharing options...
PhpxZ Posted July 9, 2010 Author Share Posted July 9, 2010 I will give you example there is a textdraw with 4 rows. after you type 8 characters at the first row it's automatic go to the next row (row 2) after you type 8 characters at the second row it's automatic go to the next row (row 3) and.. more.. Quote Link to comment https://forums.phpfreaks.com/topic/207123-how-to-limit-number-of-character-in-each-line-in-textarea/#findComment-1083482 Share on other sites More sharing options...
while1 Posted July 9, 2010 Share Posted July 9, 2010 You can limit the number of cols to 8, otherwise you'll need some JS. <textarea rows="4" cols="8"> Quote Link to comment https://forums.phpfreaks.com/topic/207123-how-to-limit-number-of-character-in-each-line-in-textarea/#findComment-1083488 Share on other sites More sharing options...
Adam Posted July 9, 2010 Share Posted July 9, 2010 You can limit the number of cols to 8, otherwise you'll need some JS. <textarea rows="4" cols="8"> That only affects the visual appearance, it has no control over the limit of characters. -- As explained in your other thread about this, it will need to be done using JS. You'll also notice there's not a lot of help about this as it's not going to be easy to achieve. Validating the entry shouldn't be difficult (split() the string by "\n" and check the length() of the array and length() of each value), but it's placing the cursor on the next line when it reaches the 8th character will be tricky. JS has little standards when it comes to cursor objects, so making it all cross-browser will be a challenge too. Have you considered an alternative? What about a series of 4 text inputs with the maxlength attribute set to 8 on each, and the name set as an array (i.e. <input type="text" name="text[]" (...)). Then on the keyup event for each, check the length of the input; if it's 8 characters long set focus to the next input. In the handling PHP if you need the text as a single string, you can just implode the array with "\n" as the delimiter. Quote Link to comment https://forums.phpfreaks.com/topic/207123-how-to-limit-number-of-character-in-each-line-in-textarea/#findComment-1083498 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.