Solved

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

Posted on 2008-10-30
5
848 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 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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