Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

18 Experts available now in Live!

Get 1:1 Help Now