• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

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

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
iyiola
Asked:
iyiola
  • 5
  • 3
  • 3
  • +1
1 Solution
 
aprestoCommented:
what exactly do you want the format button to do?
0
 
iyiolaAuthor Commented:
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
 
aprestoCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
aprestoCommented:
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
 
iyiolaAuthor Commented:
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
 
peh803Commented:
@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
 
ThogekCommented:
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
 
ThogekCommented:
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
 
iyiolaAuthor Commented:
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
 
iyiolaAuthor Commented:
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
 
ThogekCommented:
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
 
peh803Commented:
@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
 
iyiolaAuthor Commented:
Thanks for the effort  peh803. I may eb back.
I will accept Thogek answer. It works
Thanks Thogek
0
 
peh803Commented:
You're welcome iyiola -- I'm glad you got an answer that works for you.

Regards,
peh803
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now