Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET limit max characters across three text boxes and show number characters used.

Posted on 2008-10-30
5
Medium Priority
?
851 Views
Last Modified: 2013-11-07
I am creating an application to send a text message from a web page.  The text message is limited to 160 characters.  I have 3 text boxes, name (single line), number (single line), and message (multi line).

The total number of characters allowed for the 3 boxes is 160 and I would like to show on the web page the current number of characters used.

I am using VB.Net for this web project.
0
Comment
Question by:iepaul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Expert Comment

by:varungd
ID: 22839779
Try like this...
 <asp:TextBox ID="TextBox1" runat="server" onkeyup="validate();"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" onkeyup="validate();"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server" onkeyup="validate();"></asp:TextBox>
         
        <asp:TextBox ID="TextBox2" runat="server" Width="50px" ></asp:TextBox>
 
 
 
 
<script language="javascript">
    function validate()
    {
     document.getElementById('TextBox2').value=   document.getElementById('TextBox1').value.length + document.getElementById('TextBox3').value.length + document.getElementById('TextBox4').value.length;
     
    
     if(parseInt(document.getElementById('TextBox2').value) > 10)
     {
       event.srcElement.value=event.srcElement.value.substring(0, parseInt(event.srcElement.value.length) - 1);
        document.getElementById('TextBox2').value=document.getElementById('TextBox2').value -1;
     }
          
    }
    
    </script>

Open in new window

0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 22839786
Try this...

I suppose that also you have a button (named btnSend) and a Label (called lblCharacters) for showing the number of characters used.

First, in Page_Load event of the web form where the 3 text boxes are located, place this code (I'll name the text boxes as txtName, txtNumber and txtMessage):

Me.txtName.Attributes.Add("onpropertychange", "TextChanged()")
Me.txtNumber.Attributes.Add("onpropertychange", "TextChanged()")
Me.txtMessage.Attributes.Add("onpropertychange", "TextChanged()")

Second, in the HTML code of the page, place this javascript inside the <HEAD> section:
<script type="text/javascript">
    function TextChanged() {
        var txtName = document.getElementById('<%=Me.txtName.ClientID%>');
        var txtNumber = document.getElementById('<%=Me.txtNumber.ClientID%>');
        var txtMessage = document.getElementById('<%=Me.txtMessage.ClientID%>');

        //Get the numbers of characters used
        var iCharacters = txtName.value.length + txtNumber.value.length + txtMessage.value.length;
       
        //Show the number of characters in the label
        var oLabel = document.getElementById('<%=Me.lblCharacters.ClientID%>');
        oLabel.innerHTML = iCharacters;

        //Disable or enable the Send buttons if the length of the message is correct
        var sendButton = document.getElementById('<%=Me.btnSend.ClientID%>');
        btnSend.disabled = (iCharacters > 160);
    }
</script>

Hope that helps.
0
 
LVL 5

Accepted Solution

by:
varungd earned 2000 total points
ID: 22839880
please avoid previous one.  Try this...
 
In HTML
<asp:TextBox ID="TextBox1" runat="server" onpropertychange="validate();"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" onpropertychange="validate();"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server" onpropertychange="validate();"></asp:TextBox>
 
javascript

<script language="javascript">
function validate()
{
document.getElementById('TextBox2').value= document.getElementById('TextBox1').value.length + document.getElementById('TextBox3').value.length + document.getElementById('TextBox4').value.length;


if(parseInt(document.getElementById('TextBox2').value) > 10)
{

event.srcElement.value=event.srcElement.value.substring(0, parseInt(event.srcElement.value.length) - 1);

}


}

</script>
 
0
 
LVL 5

Expert Comment

by:varungd
ID: 22839892

HTML 
 
<asp:TextBox ID="TextBox1" runat="server" onpropertychange="validate();"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" onpropertychange="validate();"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server" onpropertychange="validate();"></asp:TextBox>
         
        <asp:TextBox ID="TextBox2" runat="server" Width="50px" ></asp:TextBox>
 
 
Javascript
 
 
 <script language="javascript">
    function validate()
    {
     document.getElementById('TextBox2').value=   document.getElementById('TextBox1').value.length + document.getElementById('TextBox3').value.length + document.getElementById('TextBox4').value.length;
     
    
     if(parseInt(document.getElementById('TextBox2').value) > 10)
     {
   
       event.srcElement.value=event.srcElement.value.substring(0, parseInt(event.srcElement.value.length) - 1);
      
     }
    
     
    }
    
    </script>
    

Open in new window

0
 
LVL 13

Expert Comment

by:sm394
ID: 22844550
I have build a ASP.NET  Custom Server Control in C# to deal with characters left issue
Control is multiline text box with prompt how many characters left.
If you interested i will post the code here
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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