Back to Top
The nuts and bolts for
Creative web development

Replace VbCrLf in TextArea

This is a simple tutorial on how to replace VbCrLf in a textarea by <br>, so that you can format the input when displayed. 

This tutorial will show how to replace VbCrLf with <br>, so that the input of a textarea gets formatted for display.

When making a insert or update in Ultradev/MX you just have to make a small insert to make this work.

Find the code that is commented with 'create the sql insert statement' or 'create the sql update statement'.

In this code find (Ultradev):

Else
FormVal = Delim + FormVal + Delim
End If
End If

FormVal = Replace(FormVal,vbCrLf,"<br>")

If (i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End if

In this code find (MX):

Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If

MM_formVal = Replace(MM_formVal,vbCrLf,"<br>")

If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If

Insert the red marked replace command and the flat text in a textarea will formatted where the user has made a Return.

This is very handy when using just a form to maintain for example a news admin-tool.

That's it !

 

 

Marcellino Bommezijn

Marcellino BommezijnMarcellino Bommezijn is one of the managers at dmxzone.com. He is a contributor on the tutorials section.

Owner of Senzes Media (http://www.activecontent.nl) which provides professional services and web applications for mid-sized companies.

ActiveContent CMS is the ASP.NET Content Management solution that is used for building professional and rich-featured websites.

See All Postings From Marcellino Bommezijn >>

Share this Article

Comments

RE: RE: how to do the opposite?

Remember that the Replace function has more arguments that will greatly simplify your code by allowing case INSensiTive searching

strTmp1 = Replace(CStr(strHTM),"<br>", vbCrLf, 1,-1, 1)
strTmp = Replace(strTmp1,"<tr>", vbCrLf, 1,-1, 1)
strTmp1 = Replace(strTmp,"</tr>", vbCrLf, 1,-1, 1)
strTmp = Replace(strTmp1,"<td>", vbCrLf, 1,-1, 1)
strTmp1 = Replace(strTmp,"</td>", vbCrLf, 1,-1, 1)
strTmp1 = Replace(strTmp,"<p>", vbCrLf, 1,-1, 1)
strTmp1 = Replace(strTmp,"</p>", vbCrLf, 1,-1, 1)
etc. . .

I'm sure someone could come up with a wildcard function to even further simplify this (something that looks for <?p> or <?t?> or <?br>) but you get the idea.  Also remember that xhtml is all lowercase.

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctReplace.asp

-S

RE: RE: RE: RE: how to do the opposite?

Yes! I have discovered the way to do it all serverside and show the formfield with the text already formatted:

step 1:

put the script above the <html>-tags, like this:

<%
function RemoveHTML(strHTM)
    dim strTmp, strTmp1, i, lngLen, charOne, ynWait4End
    strTmp=""
    strtmp1="CStr"(strHTM)
    'MsgBox(cbool(ynDoFormat = 1))

        strTmp1 = Replace(CStr(strHTM),"<br>", vbCrLf)
        strTmp = Replace(strTmp1,"<tr>", vbCrLf)
        strTmp1 = Replace(strTmp,"</tr>", vbCrLf)
        strTmp = Replace(strTmp1,"<TR>", vbCrLf)
        strTmp1 = Replace(strTmp,"</TR>", vbCrLf)
        strTmp = Replace(strTmp1,"<Tr>", vbCrLf)
        strTmp1 = Replace(strTmp,"</Tr>", vbCrLf)
        strTmp = Replace(strTmp1,"<tR>", vbCrLf)
        strTmp1 = Replace(strTmp,"</tR>", vbCrLf)
        strTmp = Replace(strTmp1,"<TD>", "    ")
        strTmp1 = Replace(strTmp,"</TD>", "    ")
        strTmp = Replace(strTmp1,"<td>", "    ")
        strTmp1 = Replace(strTmp,"</td>", "    ")
        strTmp = Replace(strTmp1,"<Td>", "    ")
        strTmp1 = Replace(strTmp,"</Td>", "    ")
        strTmp = Replace(strTmp1,"<tD>", "    ")
        strTmp1 = Replace(strTmp,"</tD>", "    ")
        strTmp = Replace(strTmp1,"<P>", vbCrLf)
        strTmp1 = Replace(strTmp,"</P>", vbCrLf)
        strTmp = Replace(strTmp1,"<p>", vbCrLf)
        strTmp1 = Replace(strTmp,"</p>", vbCrLf)
        strTmp = Replace(strTmp1,"<Br>", vbCrLf)
        strTmp1 = Replace(strTmp,"</Br>", vbCrLf)
        strTmp = Replace(strTmp1,"<bR>", vbCrLf)
        strTmp1 = Replace(strTmp,"</bR>", vbCrLf)
        strTmp = Replace(strTmp1,"<BR>", vbCrLf)
        strTmp1 = Replace(strTmp,"</BR>", vbCrLf)
        strTmp=""
   
 ynWait4End = False
    lngLen = Len(strTmp1)
    For i = 1 To lngLen
        charone="Mid"(strTmp1,i,1)
        If charOne="" Then
            ynWait4End = True
            charOne=""
        End If
        If ynWait4End = True Then
            If charOne = "" Then
                ynWait4End = False
            End If
            charOne = ""
        End If
        strTmp = strTmp & charOne
    next
    RemoveHTML= strTmp
    'spnDump.innerHTML=strTmp
End function
%>

then, you only have to put this in the formfield:

<%=RemoveHTML(Recordset1.Fields.Item("text").Value)%>

and voila..

RE: RE: RE: how to do the opposite?

I figured it out. The VBScript does not work on the MAC because it is client side. So I use this instead...

<%
Dim strBody
strBody = rsEntry.Fields.Item("Body")
%>
<textarea name="txtBody"><%=Replace strBody,"<br>",VbCrLf)%></textarea>

RE: RE: how to do the opposite?

This little trick works great for Win IE, but does not work on my Mac IE? Any suggestions?

See all 10 Comments