Solved

I neeed a VB script to format text in scrolling text box before submiting (URGENT)

Posted on 2004-10-27
376 Views
Last Modified: 2008-02-01
I have an HTML form below.
I want to format the text in it before submitting to database. An example witten in  "VB"  is here just to have an idea what I'm trying to do
I need this is "VBScript" only.
A working Code is what I need and will be appreciated


'=========================================================================
' Name:  FormatRemarks
'
' Notes:    This function formats raw text into CRLF dilimited lines(s)
'=========================================================================
Public Function FormatRemarks(ByVal in_strRemarks As String, ByVal in_intLimit As Integer) As String
  'On Error GoTo checkerr
 
  Dim i             As Integer
  Dim strLastChr    As String
  Dim strText()     As String
  Dim intLineCount  As Integer
  Dim intPos        As Integer
  Dim strNewText    As String


  intLineCount = LineCount(in_strRemarks)
 
  ' Determine if the last char is a carriage return/line feed
  If Right(in_strRemarks, 1) = vbLf Then
    If Mid(in_strRemarks, Len(in_strRemarks) - 1, 1) = vbCr Then
      strLastChr = vbCrLf
    Else
      strLastChr = vbLf
    End If
  ElseIf Right(in_strRemarks, 1) = vbCr Then
    strLastChr = vbCr
  End If
 
  ' Redefine the upper bound of the array according to the number of lines
  ReDim strText(1 To intLineCount)
 
  ' Loop thru the remarks and parse it into lines of text
  For i = 1 To intLineCount
    ' Get the position of the carriage return character
    intPos = InStr(1, in_strRemarks, vbCr, vbTextCompare)
   
    Select Case intPos
      Case 0 ' No carriage return char, remarks consist of 1 line of text
        strText(i) = in_strRemarks
        in_strRemarks = ""
      Case 1 ' The line start out with the carriage return character
        strText(i) = ""
      Case Else
        strText(i) = Left(in_strRemarks, intPos - 1)
    End Select
   
    If in_strRemarks <> "" Then
      ' Check if the next character is the line feed character
      If Mid(in_strRemarks, intPos + 1, 1) = vbLf Then
        in_strRemarks = Mid(in_strRemarks, intPos + 2)
      Else
        in_strRemarks = Mid(in_strRemarks, intPos + 1)
      End If
    End If
  Next

Thanks



<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>

<form method="POST" action="save.aspx--">
  <p><textarea rows="2" name="S1" cols="20">Text to format Text to formatText to format Text to format Text to format Text to format Text to format Text to format Text to format Text to format Text to format Text to format Text to format Text to format Text to format</textarea></p>
  <p><input type="button" value="Format" name="B3"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>
0
Question by:iyiola
    14 Comments
     
    LVL 23

    Expert Comment

    by:apresto
    what exactly do you want the format button to do?
    0
     

    Author Comment

    by:iyiola
    Hi apresto


    When the format button is clicked.  I want to call a function to do the following:

    I want the text to "wrap"  or put a page break at 50th characted.

    If the text is less than 50 character, then do nothing
    If text is very long, eeach line must wrap (page break) at 50th character
    Thanks
    0
     
    LVL 23

    Expert Comment

    by:apresto
    this COuLD Work:

    If Len(Request.Form("textarea")) >= 50 Then
        response.write "<BR><BR>"
    End If

    I was looking around and this Len thing did the job for some others?
    0
     
    LVL 23

    Expert Comment

    by:apresto
    If Len(Request.Form("S1")) >= 50 Then
        response.write "<BR><BR>"
    End If

    forgot to change it, this will need to be in the page that the info is submitted to
    0
     

    Author Comment

    by:iyiola
    This will not work for me.
    I need this to be a function.The onlick event of teh Format button will call the function and format the text
    0
     
    LVL 19

    Expert Comment

    by:peh803
    @iyiola:

    I would like to understand your requirements better.  You say that you'd like this data handled before you insert it into your database, right?  Well, consider a 200 character string where every 50th character falls in the middle of a word.  Is this acceptable?  Do you still want to break to a new line at this point exactly, or do you want to go to the closest space?

    Also, do you want line breaks for textarea / plain text email display purposes (vbCfLf) or for HTML display purposes (<BR>)?

    Thanks,
    peh803
    0
     
    LVL 15

    Expert Comment

    by:Thogek
    If you need the formatting to occur before submitting, then it's happening client-side, right?  Any reason why it has to be VBScript, then?  (Especially given that JavaScript is better supported across different browsers.)
    0
     
    LVL 15

    Expert Comment

    by:Thogek
    Question:
    What if there are already line-breaks within the textarea?  Do you want to leave those alone, and just restart your line-wrap formatting with eaach new line/paragraph?
    0
     

    Author Comment

    by:iyiola
    Hi peh803
    Yes I would like it handled before inserting into database. I'm more concerned about formating than the database part.
    Yes it acceptable to have the characted fall in the middle given teh example of 200 characted string.
    It's just the requirement
    This is not for email purpose, just readability by users instead of scolling. That's the purpose
    Will appreciate your help.
    I need this in VBScript. A working code is much appreciated
    0
     

    Author Comment

    by:iyiola
    Hi Thogek,
    Yes I need this in VB Script. If you have a solution in Javascript, it's better than nothing though.
    The line-wrap formating function should overide the line -breaks within the textarea
    0
     
    LVL 15

    Accepted Solution

    by:
    Here's one quick pass:

        <script language="VBScript">
        Function FormatTextArea(ByVal oTextArea)
            Dim strText, strFormatText
            strText = Replace(oTextArea.innerText, vbCrLf, " ")
            strFormatText = ""
            While Len(strText) > 50
                strFormatText = strFormatText & Left(strText, 50) & vbCrLf
                strText = Right(strText, Len(strText) - 50)
            WEnd
            strFormatText = strFormatText & strText
            oTextArea.innerText = strFormatText
        End Function
        </script>

        <form>
        <textarea rows="2" name="S1" cols="60"></textarea><br />
        <input type="button" value="Format" language="VBScript" onclick="FormatTextArea(me.form.S1)" />
        </form>

    Noting, of course, that being in VBScript, this will not work in Netscape (or most other non-IE) browsers.
    0
     
    LVL 19

    Expert Comment

    by:peh803
    @iyiola,

    it appears that thogek has posted a good solution for you.  I am more than happy to help, however; so please just let me know if you need more help.  Obviously, if thogek's solution works for you, no need to post extraneous stuff.

    regards,
    peh803
    0
     

    Author Comment

    by:iyiola
    Thanks for the effort  peh803. I may eb back.
    I will accept Thogek answer. It works
    Thanks Thogek
    0
     
    LVL 19

    Expert Comment

    by:peh803
    You're welcome iyiola -- I'm glad you got an answer that works for you.

    Regards,
    peh803
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now