...

View Full Version : Textarea and Capturing



talonnb
04-30-2011, 04:10 AM
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

bullant
04-30-2011, 05:11 AM
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.



<!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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum