I built a very simple app using a PHP form with a bit of Javascript.

In my form, I have a text input which I used to run a database search. In case I have multiple values, I have a bit of code that puts a comma in between each.

The weird part is this:

In Firefox, I can go do MS Excel, copy 5 values and paste them in the text input control. I can see all 5 values pasted and commas in between.

In Internet Explorer version 8, I can go do MS Excel, copy 5 values but only ONE value (the first number) gets pasted in the text input control.

This is my html

<fieldset>
<label for="DBRIDs">RIDs</label><input type="text" id="DBRIDs" name="DBRIDs" onchange = "removespaces(this)">
</fieldset>

This is my Javascript in my page header

<script language="javascript" type="text/javascript">
function removespaces(which) {
str = which.value;
str = str.replace (/\s|\n/g,","); // replace space or newline by commas
document.myform.DBRIDs.value = str;
}

Pretty basic stuff.

Tried changing textinput to textarea. No change. Am able to paste without problems from Firefox, Chrome, Safari and even Opera! IE is a no go.

What am I missing? How come IE cannot paste like Firefox?

Maybe something like this:

str = str.replace(/\s/g,",").replace(/\r\n/g,",").replace(/\n/g,",");

When you have it working, then think about squeezing the parts into a single regexp.

Airshow

I had a typo so using textarea is working now. I can copy a column and paste it from IE

Of course (sarcasm), it is introducing a new problem:

It duplicates my commas and I am unclear it's because of textarea or my Javascript:

function removespaces (which) {
    var str = which.value;
    str = str.replace(/\s|\n/g, ","); // replace space or newline by commas
    which.value = str;
}
function handlePaste (which) {  
    var str = window.clipboardData.getData("Text");    
    str = str.replace(/\s|\n/g, ","); // replace space or newline by commas
    which.value = str;
    return false; // kill the paste event so you don't get duplicate data.
}
...
<textarea id="DBRIDs" name="DBRIDs" rows="1" cols="20" onchange="removespaces(this)" onpaste="handlePaste(this)"></textarea>

Any idea?

str = (/*b.b. Troy III p.a.e.*/String(str).split(/\s+/).join());

In removeSpaces(), try:

str = str.replace(/\s+|\n/g, ","); // replace space or newline by commas

I don't recognise the onpaste event so can't work out how/when handlePaste() is supposed to work.

Airshow

I figured out what's wrong.

1) my html was incorrect ; onpaste is not a recognized event of textarea so here is the correct version

<textarea id="DBRIDs" name="DBRIDs" rows="3" cols="20" onchange="removespaces(this)"></textarea>

2) but it introduced a new problem by giving me double commas ; I corrected my regex and use the slice method to remove the last comma

function removespaces (which) {
    var str = which.value;
    str = str.replace(/\s+/g, ",");
    var newStr = str.slice(0, -1)
    which.value = newStr;
}

I am in business. Thanks for your help.

In removeSpaces(), try:

str = str.replace(/\s+|\n/g, ","); // replace space or newline by commas

I don't recognise the onpaste event so can't work out how/when handlePaste() is supposed to work.

Airshow

-you've made yourself a redundant code there double effort at least 4x more expensive.
your regexp is sort of an equivalent of say: /John|J|o|h(...etc)/
which is wrong.

Hi Troy.

Your split().join() is much better.

I wouldn't have posted my suggestion if I had seen your post first.

Airshow

I somehow managed to drop the main line from my feedback and the reason I wrote my last post which was to be stating that \n, \t, \r and similar, are all white-spaces equal to \s.
But wouldn't go into explaining that \s+ will also match double or more inline white-spaces including their combinations appearing successively in your string.

I've added String constructor to it to make sure it is not running on a plain stream of char-tokens which is a possible situation to run into when copying and pasting from text areas or other similar text sources where \n and other family members happen to be represented literally with "\n" and other corresponding escaped characters.

For leading and trailing white-spaces advise the trim() method to be used as a Preparator agent.

The split() method fed with the (/\s+/) RegExp will make sure to return a qualitative Array consisting of bounds only. And since Arrays use a comma-separator syntax for their values, returning a duck typed array into a string representation will contain the required commas on its end-result by default.
This way the join() operation added there would become optional, or unnecessarily redundant; when arg is omitted [its default join char is also a comma],- but it is there for precaution measures since there are cases when array feed may not necessarily undergo a type conversion - and there's no harm keeping it there as an extension, even if you are sure that a desired duck-type conversion is immanent, cause it will make it ready for instant modification of a join/separator character so you'll be able to insert a (dot) "." join instead, or whatever else you may need to on the fly. And;

This is what appears to be my 2 last cents on the topic.
Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.