Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    TextArea Validation with Word Wraps

    i have a text area with max length of 250 and the text area must have allow only 50 characters in each of the 5 lines. if a particular word is not coming fully in the 50 characters it can wrap to the next line. so i may not be using the full 50 character space in each of the 5 lines.
    Question: how do i achieve it. I want to count the number of rows by identifying the rows(with new line or by word wrap if the word gets wraped or by new line if it does not) then if he has reached 5 rows though he can type more character out of 250 he should not be able to go to the next line. Can some one pls provide your suggestions.

    function findRows()

    oArea = document.getElementById('txtAreaId');
    var aNewlines = oArea.value.split("\n,\r");
    var iNewlineCount = aNewlines.length;



    <td ><textarea name="txtArea" id="txtAreaId" COLS="50" ROWS="5" WRAP="HARD" class="REQUIRED" style="width: 100%;" onkeypress="return

    findRows();" >

  2. #2
    Join Date
    May 2002
    Hayward, CA
    Thanked 24 Times in 22 Posts
    This question's in the wrong forum - Post a JS is supposed to be for completed, working scripts. I'm interested in a solution, though - anytime text processing comes up, I want to know about the algorithm used to handle it... so I'll watch. Sorry I couldn't help outright.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, ES7-Membrane project (Github Pages site)
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary

  3. #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Thanked 586 Times in 580 Posts


    I'm not sure what your final project is trying to accomplish,
    but I'm pretty sure you won't get there with that code.

    Try this...
    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8" />
    <title> Untitled </title>
    <textarea id="txtArea" COLS="50" ROWS="5" WRAP="HARD" class="REQUIRED" onkeypress="return findRows();" ></textarea>
    <div id="newLines"></div>
    <script type="text/javascript">
    function findRows() {
      var TArea = document.getElementById('txtArea').value;
      var aNewLines = TArea.split(/\n|\r/);
      var tarr = TArea.split('');
      document.getElementById('newLines').innerHTML = '# of new lines: ' + aNewLines.length + ' and has '+(tarr.length+1)+' characters';
    Note: The <textarea> does not produce a \n or \r
    when words are typed continuously.
    You can see this (if using FF) by expanding the area with a mouse click
    on the lower right corner of the textarea.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts