Javascript textarea cleanup from old textarea to new textarea

Hello EE,
I am receiving a paste of item numbers into a form textarea...probably strait from Excel, but who knows.  When the user pastes or enters data into the old text area I want cleaned items to be written to a new textarea (for passing as a string.).  This form will be on IE only.  Here's the questions:  
* Can this happen live or do I need to tie this to a submit button?
* The old textarea "paste" may contain spaces, special characters, line feeds, commas.
* The new textarea should have each cleaned item set to 10 digits (zero filled or left as is if they are bigger) and  separated by a single comma (in case of multiple line feeds, spaces, etc.)

I've gotten this far, but broke something.  For adding the zeroes I figured I could use a combination of length and concat, but I don't know how to repeat the zeroes.

Any direction would be appreciated!
LVBarnes

function getItems(id) {
        var textArea = document.getElementById(id);
        var words = textArea.value.replace(/[^a-zA-Z0-9]/g, ' ').split(/\s+/);
        var words = textArea.value.replace('  ', ' ').split(/\s+/);
        document.getElementById('new').value = words.join(',');
    }
LVL 5
Lawrence BarnesAsked:
Who is Participating?
 
Kim WalkerWeb Programmer/TechnicianCommented:
I believe the line
var words = textArea.value.replace(/[^a-zA-Z0-9]/g, ' ').split(/\s+/);

Open in new window

will replace any character in the textarea field that is not a number or letter with a space, split the entire string at each space and store it in the variable words. (Like I said, I'm not so good with regular expressions.) The characters in the textarea field have NOT been changed at this point. The edited text is in the variable words.

The next line
var words = textArea.value.replace('  ', ' ').split(/\s+/);

Open in new window

will replace any two consecutive spaces in the textarea field with a single space, split the entire string at each space and destroy what was done in the previous line by replacing the array stored in words with this new array.

I believe these two lines can be combined as
var words = textArea.value.replace(/[^a-zA-Z0-9]/g, ' ').replace('  ', ' ').split(/\s+/);

Open in new window

Follow this line with the nested loops I posted earlier
function getItems(id) {
    var textArea = document.getElementById(id);
    var words = textArea.value.replace(/[^a-zA-Z0-9]/g, ' ').replace('  ', ' ').split(/\s+/);
    for (var i = 0; i < words.length; i++) {
        word = words[i];
        while (word.length < 10) {
            word = '0' + word;
        }
        words[i] = word;
    }
    document.getElementById('new').value = words.join(',');
}

Open in new window

0
 
Kim WalkerWeb Programmer/TechnicianCommented:
For padding numbers, you can use a while loop:

while (str.length < 10) {
    str = '0' + str;
}

Open in new window

0
 
Lawrence BarnesAuthor Commented:
Thanks...can you help me get that into the function above?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Kim WalkerWeb Programmer/TechnicianCommented:
I'm not very good with regular expressions, but in your function you are declaring the variable words twice. The second declaration is overwriting the first. You should fix that, I'm not sure what you are trying to accomplish there.

Once you have the words array set up properly, you can insert before your last line:

for (var i = 0; i < words.length; i++) {
    word = words[i];
    while (word.length < 10) {
        word = '0' + word;
    }
    words[i] = word;
}

Open in new window

0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Also, you can attach this script to an onchange event on your input textarea element without having a button, but you would have to click outside the textarea or tab outside it for the script to execute.
0
 
Lawrence BarnesAuthor Commented:
Here's my goal:
*****Input by user into textarea *****
12345,54321  23451
512#

45123
*****************************************
******Javascript writes cleaned up and zero filled string into a different textarea****
0000012345,0000054321,0000023451,00000512,0000045123
****************************************************************************
0
 
Lawrence BarnesAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.