Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I currently have a field in my database which allows 3000 to be entered.

On the front end i have a textbox in which users enter the data.

I want to limit the amount of charaters to 55 per line when users are entering the data

e.g.

this is some text(55 Chara)line1
this is some text(55 Chara)line2
this is some text(55 Chara)line3
this is some text(20 Chara)line4

All of the above should be in the same textbox
Posted
Updated 15-May-14 21:27pm
v2
Comments
DamithSL 16-May-14 2:10am    
is this windows form or web application?
isi19 16-May-14 2:50am    
Web Application
Sampath Lokuge 16-May-14 2:38am    
Can you put the code snippet of your text box ?
[no name] 16-May-14 3:09am    
This is multiline textbox or not? and window app or web app?
isi19 16-May-14 3:32am    
Yes it is a multiline textbox and its a web app

You probably need to wrap your text. Check this

How to wrap text in a textBox on a web page[^]
 
Share this answer
 
You can use Javascript function to achieve this. I have created one dummy javascript function for achieving this. Please find below same.

JavaScript
// Javascript function to move the text to next line in textbox if it exceeded the limit of maximum charcaters in one line i.e. maxCharInLine 

 <script type="text/javascript">
          function lineLimit(maxCharInLine) {
              var txtDemo = document.getElementById("<%=txtDemo.ClientID%>");
              if (txtDemo.value.lastIndexOf('\n') != -1 || txtDemo.value.length >= maxCharInLine)
              {
                  var trailingText = txtDemo.value.substr(txtDemo.value.lastIndexOf('\n') + 1);
                  if (trailingText.length >= maxCharInLine)
                  {
                      txtDemo.value = txtDemo.value + '\n';
                  }
              }
        }
    </script>

// In declaration of textbox, you have to call above function "lineLimit" on key press and also define the maximum limit of character for one line. Here I have defined 5 as maximum character limit. 

  <asp:TextBox ID="txtDemo" runat="server" TextMode="MultiLine" onkeypress="javascript:lineLimit(5);"></asp:TextBox>


I hope this will help you :) :)

Enjoy Coding.. :)
 
Share this answer
 
v2
Aspx page:
XML
<asp:Label ID="lblMessage" runat="server" ForeColor="Red" EnableViewState="false" />
    <p>
        <asp:TextBox ID="txtDetails" runat="server" TextMode="MultiLine" Columns="50" Rows="5" /></p>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitData" />

cs file:
C#
protected void SubmitData(object sender, EventArgs e)
    {
        // allow only 500 characters
        var maxLength = 50;
        if (txtDetails.Text.Trim().Length > maxLength)
        {
            lblMessage.Text = string.Format("Sorry, only {0} characters are allowed.",
            maxLength);
            return;
        }
        // go ahead and write code to save the data
    }

hope this help you
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900