-1

i have a question, i need to figure out how to split a text inputted in a text area but at the moment of typing is only visually ordered, i'll explain it in detail.

a determined user types in a text area something like this

this is how the input looks like

1- sample text 1.
2- sample text 2.
3- sample text 3.

that is how the user would type it in the textbox, but when i display it looks like this (note that not all users end their writing with a dot)

1- sample text 1. 2- sample text 2. 3- sample text 3.

how can i separate them visually and display it as a list? i tried replace, mid, and other functions but did not work, thanks in advance

user692942
  • 16,398
  • 7
  • 76
  • 175
  • 4
    Does this answer your question? [Is there "\n" equivalent in VBscript?](https://stackoverflow.com/questions/2203710/is-there-n-equivalent-in-vbscript) – Flakes Sep 17 '20 at 04:25
  • The line-break will still be there it just has no affect when rendering HTML (if you check the page source you'll likely see the individual lines) so use `lines = Spilt(sampletext, vbNewLine)` which will set the `lines` variable to an `Array` you could iterate over and generate a `
      ` (unordered list).
    – user692942 Sep 17 '20 at 06:52

3 Answers3

2

If you simply want to display it like it was typed without too much extra hassle, you can use replace() in one single line of code:

replace(inputFieldString, vbCrlf, '<br>')

Some of our older systems use this method for content from textarea inputs. Quick and easy.

Daniel Nordh
  • 362
  • 3
  • 15
  • That is covered by the dup target - [Is there "\n" equivalent in VBscript?](https://stackoverflow.com/a/40866272) – user692942 Sep 28 '20 at 15:27
1

Try this instead, split the text at line-breaks and loop over the array to create the list:

function addText() {
  let list = document.querySelector('#list');
  let text = document.querySelector('#text').value;
  let values = text.split('\n');

  let listFragment = new DocumentFragment();

  values.forEach(value => {
    let span = document.createElement('span');
    span.textContent = value + ' ';
    listFragment.append(span);
  });

  list.append(listFragment);
}
<textarea id="text"></textarea>
<button onClick="addText()">enter</button>
<div id="list"></div>
user692942
  • 16,398
  • 7
  • 76
  • 175
  • It makes zero sense to do this on the client-side in a Classic ASP environment, maybe if you are building a SPA perhaps. – user692942 Sep 17 '20 at 09:45
1

Because HTML ignores whitespace you won't see the line breaks, but you can confirm they are there, by viewing the browser page source.

If you want to take the text and output it as an unordered list (<ul>) for example you could do something like this;

<%
Option Explicit

'Starting point for the script.
Call init_page()

Sub init_page()
  Dim issubmit: issubmit = (Request.Form.Count > 0)
  Dim value: value = Request.Form("sampletext") & ""

  Call build_form(value)
  If issubmit Then
    Call Response.Write(build_list(value))
  End If
End Sub

Sub build_form(value)
%>
<form method="POST">
  <textarea name="sampletext" rows="5"><%= value %></textarea>
  <button>Submit</button>
</form>
<%
End Sub

Function build_list(value)
  Dim html, list, item, items
  If Len(value) < 1 Then build_list = ""
  list = Split(value, vbNewLine)
  If IsArray(list) Then
    html = "<ul>"
    items = UBound(list)
    For item = 0 To items
      html = html & "<li>" & list(item) & "</li>"
    Next
    html = html & "</ul>"
  Else
    html = "Error: List was not in the correct format"
  End If
  build_list = html
End Function
%>

The example is a single page ASP page script that posts the form to itself and uses a function to take the posted text, convert it to an array using Split() and use the array to build an unordered list that is written back to the page. This is by no means a solution, it is intended as a training exercise that should give you some idea of how to do this in your own project.

user692942
  • 16,398
  • 7
  • 76
  • 175