Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    TextArea Validation with Word Wraps

    hi,
    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;

    alert(aNewlines);

    }


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

    findRows();" >
    </textarea></td>

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,460
    Thanks
    1
    Thanked 23 Times in 21 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, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    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...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title> Untitled </title>
    
    </head>
    <body>
    <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';
    }
    </script>
    
    </body>
    </html>
    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
    •