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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lawrence BarnesAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.