function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jay reddyJay reddy 

javascript error not allow line breaks in the text input field

Hello,

I'm getting javascript error when entering a text with line break in a field, which is mentioned in the javascript code. Is there a way to skip that error from a line break. I used JSENCODE but it's working for special characters only and not for line breaks. Below is the way I have defined the field in Javascript.
 
var field = '{!JSENCODE(Opportunity.Closing_instructions__c)}';
field = field.replace(/(/\r?\n|\r/),"");
Thanks,
GR
 
Raj VakatiRaj Vakati
Hi , 
Please try this 
var field = '{!JSENCODE(Opportunity.Closing_instructions__c)}';

field = field (/(\r\n|\n|\r)/gm,"");
alert(field);

 
Shruti SShruti S
I was able to replicate this problem in my Org. Here is my code -
<apex:page standardController="Contact">
    <script>
        var description = "{!Contact.Description}";
    </script>
</apex:page>

Here is a screenshot -

User-added image



I then used JSENCODE and I was able to solve the issue. Here is the code -
<apex:page standardController="Contact">
    <script>
        var description = "{!JSENCODE(Contact.Description)}";
    </script>
</apex:page>

Here is a screenshot - 
User-added image

In my example, I have used Text Area type of field. Are you also using Text Area type? Please let me know.
Jay reddyJay reddy
@shruti S

Hi .. Thanks for the reply. As I mentioned in my question, I have used the JSENCODE in my code. But it's still throwing me an error whenever I give line breaks in the Text field.

Secondly, I have my code in the custom button. Furthermore, my field is Long Text Area field.

Thanks,
GR