Skip to main content
Known Participant
July 23, 2021
Answered

Pasting from excel adds a space & the JS doesn't recognise it

  • July 23, 2021
  • 1 reply
  • 754 views

I have a drop down with 80+ code numbers in it. Depending on the code chosen, a specific sentence appears further down the document using this JS (obviously only part of it):

 

var selectedProducts = this.getField("Code off 2").value;
if (selectedProducts=="...") event.value = " ";
else if (selectedProducts=="A21305" || selectedProducts=="A40005" || selectedProducts== "A50005" || selectedProducts=="A55005" || selectedProducts=="A80005" || selectedProducts=="A90105" || selectedProducts=="A90205" || selectedProducts=="A92005" || selectedProducts=="A99005") event.value = "Remise de 5% sur tarif en vigueur des encres et étiquettes";

 

The drop down is also set to accept custom text so the users can copy and paste the code from an excel sheet. This is where the problem is happening. If you copy the cell, it includes a space after the number so the JS for the sentence doesn't recognise this. If you remove the space, it works fine.

 

Does anyone know of an easy fix for this please? I don't know enough to make adjustments to the JS to make it pick up the code with and without a space, other than literally duplicating what's there.

 

Thank you!

 

This topic has been closed for replies.
Correct answer try67

Yes, this is a known issue with Excel.

There are a couple of ways to do it. You can strip the space when pasting the text into the field, or you could adjust your code to do so. I think the former is better, as it will keep the value of your field "clean", and you won't need to adjust your code.

To do that you can use this code as the field's custom validation script:

event.value = event.value.replace(/^\s+/,"").replace(/\s+$/,"");

1 reply

try67
try67Correct answer
Community Expert
July 24, 2021

Yes, this is a known issue with Excel.

There are a couple of ways to do it. You can strip the space when pasting the text into the field, or you could adjust your code to do so. I think the former is better, as it will keep the value of your field "clean", and you won't need to adjust your code.

To do that you can use this code as the field's custom validation script:

event.value = event.value.replace(/^\s+/,"").replace(/\s+$/,"");

bebarth
Community Expert
July 25, 2021

Can also be written:

event.value = event.value.replace(/^\s+|\s+$/g, "");

jlehaneAuthor
Known Participant
July 26, 2021

Thank you also.