Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

newline character on text area

I have a custom field called Current_Address__c which is of datatype textarea.

I need to populate this field in the format below. ie a newline char after street and another newline after zip.

street City state Zip Country

The values of city state zip country etc are been taken from contact object. I dont want to use this as a formula field. So i need to populate it in my controller and display it on my VF page.

I am trying to add a newline char by using the code below

this.customobj.Current_Address__c = currentStreet + '\\n ' + currentCity + ' ' + currentState  + ' ' + currentZIP  + '\\n ' + currentCountry ;

i had also used \n instead of \n.

It still show the field in one line instead of 3 lines

EDIT

I got this working using the following code. I would accept mathews answer as it would work with outputfield.

                currentAddress = currentStreet;
            currentAddress += '\r\n';
            currentAddress += currentCity + + ' ' + currentState  + ' ' + currentZIP ;
            currentAddress += '\r\n';
            currentAddress += currentCountry;

This works only if you use +=. not sure why this happens

like image 446
Prady Avatar asked Apr 10 '12 05:04

Prady


People also ask

Which character defines a new line in a text area?

Talking specifically about textareas in web forms, for all textareas, on all platforms, \r\n will work.

How do you write a newline character?

There are two basic new line characters: LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.

How do you add a new line character in HTML?

<br>: The Line Break element. The <br> HTML element produces a line break in text (carriage-return).


2 Answers

I think I found the issue, you have two escape character slashes (\\n), but only one is needed because the slash in \n does not need to be escaped in this context.

Also, Salesforce saves a new line as \r\n. Try this:

this.customobj.Current_Address__c 
    = currentStreet + ' \r\n' 
    + currentCity + ' ' + currentState  + ' ' + currentZIP  + ' \r\n' 
    + currentCountry;

This method works when using an <apex:outputfield> with an sObject field.

<apex:outputtext value="{!myCustomSObject__c.Address__c}"/>

If you're using a different Visualforce Component, it won't work. Visualforce renders the new line in HTML when using a <apex:outputtext> Component, but HTML ignores new lines. If you use a <br/> tag, Visualforce renders it as &lt;br/&gt;.

The best solution I could come up with for rendering a variable that has new lines in it (rather than an sObject field) is to use a disabled <apex:inputtextarea>.

<apex:inputtextarea value="{!myAddress}" disabled="true" readonly="true">
</apex:inputtextarea>
like image 77
Matt K Avatar answered Oct 10 '22 08:10

Matt K


Recently I had the same problem, I wanted to reder the new lines in an The solution that I found was this, it is a little tricky but it works:

<apex:outputText value="{!SUBSTITUTE(JSENCODE(textVariableThanContainsNewLines), '\\n', '<br/>')}" escape="false"/>
like image 3
Marcos Montaño Avatar answered Oct 10 '22 10:10

Marcos Montaño