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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Textarea and Capturing

    Basically I have a text area where I want to be able to copy/paste in text that looks like this:

    111212 54.43
    111210 54.41

    and sometimes

    -1.00 111212 454.33
    -1.00 111012 453.21

    Both have a set number of blank spaces in front of the date and in between the values plus a line break seperating the lines, and both of these options are similar to what would be copied into this text field. In the first example, I'd like to capture the 4 values. In the second, I'd like to ignore the -1.00 and just capture values like in the first example. I've tried regex matching to no avail, but I must admit I've not really ever mastered Regex so any help on both match lines or an easier way to get these values from a textarea would be appreciated. I do need the values seperately so the general text value doesn't give me what I need.

    Thanks in advance
    Last edited by talonnb; 04-30-2011 at 04:12 AM.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    You can do it without regex. Maybe use this demo as a guide. Because you can have just 1 or multiple rows of data, I put all the extracted numbers into a 2D array, rowData ,where each row in the array contains the extracted numbers from each row in the textarea.

    At the end I output the contents of rowData in an alert() for verification purposes.

    This demo doesn't include any data validation to check if valid data exists on each row.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title></title>
            <style type="text/css"></style>
            <script type="text/javascript">
                function getValues(){
                    var str = document.getElementById('txt1').value;
                    var rowData = new Array();
                    if(str.indexOf('\n') != -1){ //we have multiple rows of data
                        var strRows = str.split('\n');
                        for(i=0; i < strRows.length; i++){
                            var strRowsA = strRows[i].split(' ');
                            rowData[i] = new Array();
                            var startIndx = (strRowsA.length > 2)? 1 : 0;
                            for(j=startIndx; j < strRowsA.length; j++){
                                rowData[i].push(new Number(strRowsA[j]));
                            }
                        }
                    } else { //only single row of data
                        var strRowsA = str.split(' ');
                        rowData[0] = new Array();
                        var startIndx = (strRowsA.length > 2)? 1 : 0;
                        for(j=startIndx; j < strRowsA.length; j++){
                            rowData[0].push(new Number(strRowsA[j]));
                        }
                    }
                    //output results for testing
                    var strOut = '';
                    for(i=0; i < rowData.length; i++){
                        for(j=0; j < rowData[i].length; j++) {
                            strOut += rowData[i][j]+'  ';
                        }
                        strOut += "\n";
                    }
                    alert(strOut)   
                }
                window.onload=function(){
                    document.getElementById('btnGetValues').onclick=getValues;
                }
            </script>
        </head>
        <body>
            <div>
                <textarea id="txt1" cols="20" rows="5"></textarea>
                <button id="btnGetValues">Get values</button>
            </div>
        </body>
    </html>
    Last edited by bullant; 04-30-2011 at 05:21 AM.


  •  

    Posting Permissions

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