Solved

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

Posted on 2008-10-30
5
842 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
  • 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now