hi guys,

i want to give new line after fixed amount of characters(say after every 100 characters) in the i have to display my news content which is very large.

pl guide me for that.thanks waiting for ur reply.

Dani AI

Generated

Two common approaches depending on intent: either let the browser wrap visually (do not change the textbox value), or actually insert newline characters into the value so the saved text contains breaks.

If the goal is only readable display, use the textarea's built‑in wrapping (set cols or the wrap attribute) or CSS like white-space: pre-wrap so the text appears broken into lines without modifying the stored string. See the textarea element and the CSS white-space property on MDN for current behavior and options (textarea element, white-space).

's keypress-counter idea can work in a very simple typing scenario, but it misses important cases: paste, selection replacement, backspace/replace, mobile/IME input and cross‑browser key events. Those lead to incorrect counts or unexpected cursor jumps. Better choices are:

  • Format on the client when the user finishes typing (on blur or before submit), or
  • Format on the server when saving/displaying, or
  • If you need live formatting, use the input event and operate on the whole value while preserving caret position.

Examples (safe, minimal patterns to adapt):

/* JS: wrap by chunks (use on input/blur; preserve selection manually when needed) */
function wrapEveryN(value, n) {
  return value.replace(new RegExp('.{1,' + n + '}', 'g'), '$&\n');
}
/* C#: server-side chunking before saving or sending to client */
string WrapEveryN(string s, int n) {
  if (s == null) return s;
  var sb = new System.Text.StringBuilder();
  for (int i = 0; i < s.Length; i += n) {
    int len = Math.Min(n, s.Length - i);
    sb.Append(s.Substring(i, len));
    if (i + len < s.Length) sb.Append("\n");
  }
  return sb.ToString();
}

Notes: prefer formatting on blur/submit for reliability; consider wrapping at word boundaries (find last space before the limit) if readable text is desired; and remember browsers normalize textarea newlines, so use LF (\n) consistently.

hey dude you can use this javascript, i have tried it in sample application. just change the "[COLOR="Red"]100[/COLOR]" with your requirement. I mean after the number of character you want [B]new line[/B].

<script language="javascript" type="text/javascript">
    var cntChar = 0    
    function GetKeyPress()
    {        
        if(cntChar == 100)
        {
            document.getElementById('TextBox1').value = document.getElementById('TextBox1').value + '\r\n';
            cntChar = 1;
        }
        else
        {
            cntChar = cntChar + 1;
        }        
    }
</script>

And in the Page_Load event. Add the below line..

TextBox1.Attributes.Add("OnKeyPress", "GetKeyPress();")[/COLOR]

hope it will solve your problem :-)

hi guys,

i want to give new line after fixed amount of characters(say after every 100 characters) in the textbox.as i have to display my news content which is very large.

pl guide me for that.thanks waiting for ur reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.