locked
Function textbox Counter and other operations RRS feed

  • Question

  • User1426870613 posted

    Hello,

    I have a question: I create a function to count the number of caracters of a textbox. To report the number of caracters you have avaliable. When I load the screen works. But when I click a button to clean the screen the function doesn't work anymore. Another case is when I use a function with dialog to open a screen that uses the same function. It does not work.

    Follow the code in Java

                $('<%[textbox].ClientId%>').keypress(function () {
                    contFunction()
                });

               function contFunction() {
                var maxlimit = 949;
                var label = document.getElementById('<%=[label].ClientID %>');
                var textbox = document.getElementById('<%=[textbox].ClientID %>');
                 if (textbox.value.length > maxlimit)
                     textbox.value = textbox.value.substring(0, maxlimit);
                 else
                     label.innerText = (maxlimit - textbox.value.length);
            }

    Anybody has a idea what I did wrong?

    Thanks, for the attention.

    Monday, October 13, 2014 3:26 PM

Answers

  • User281315223 posted

    You need to ensure that you have your existing jQuery code wrapped within a "document-ready" block and that you include the '#' character prior to retrieving your ClientID property as seen below :

    <script type='text/javascript'>
        // When your page loads, wire up these events
        $(function(){
             // When the keypress event for your Textbox is triggered, call your event
             $('#<%= YourTextBox.ClientId %>').keypress(function () {
                    contFunction()
             });
    
             // You might also want to consider triggering it when the textbox loses focus, on the blur event
             $('#<%= YourTextBox.ClientId%>').blur(function () {
                    contFunction()
             });  
        });
    
        // Define your function
        function contFunction() {
                // Define your max number of characters
                var maxlimit = 949;
    
                // Since you are already using jQuery, no need to stop now
                var $label = $('#<%= YourLabel.ClientID %>');
                var $textbox = $('#<%=YourTextBox.ClientID %>');
    
                // Check if your value exceeded the maximum
                 if ($textbox.val().length > maxlimit){
                     // Trim it
                     $textbox.val($textbox.val().substring(0, maxlimit));
                 }
                 
                 // Display the number of characters remaining
                 $label.text(maxlimit - $textbox.val().length);
        }
    </script>

    You can see a complete working example available here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 13, 2014 3:48 PM

All replies

  • User281315223 posted

    You need to ensure that you have your existing jQuery code wrapped within a "document-ready" block and that you include the '#' character prior to retrieving your ClientID property as seen below :

    <script type='text/javascript'>
        // When your page loads, wire up these events
        $(function(){
             // When the keypress event for your Textbox is triggered, call your event
             $('#<%= YourTextBox.ClientId %>').keypress(function () {
                    contFunction()
             });
    
             // You might also want to consider triggering it when the textbox loses focus, on the blur event
             $('#<%= YourTextBox.ClientId%>').blur(function () {
                    contFunction()
             });  
        });
    
        // Define your function
        function contFunction() {
                // Define your max number of characters
                var maxlimit = 949;
    
                // Since you are already using jQuery, no need to stop now
                var $label = $('#<%= YourLabel.ClientID %>');
                var $textbox = $('#<%=YourTextBox.ClientID %>');
    
                // Check if your value exceeded the maximum
                 if ($textbox.val().length > maxlimit){
                     // Trim it
                     $textbox.val($textbox.val().substring(0, maxlimit));
                 }
                 
                 // Display the number of characters remaining
                 $label.text(maxlimit - $textbox.val().length);
        }
    </script>

    You can see a complete working example available here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 13, 2014 3:48 PM
  • User1426870613 posted

    This works with ASP:Content and Visual Studio 2012?

    Tuesday, October 14, 2014 8:55 AM
  • User281315223 posted

    This works with ASP:Content and Visual Studio 2012?

    Yes absolutely. Since it relies on Javascript / jQuery, it is completely agnostic to whatever server-side technology you are using (e.g. ASP.NET, Ruby, PHP, etc.). 

    Tuesday, October 14, 2014 9:29 AM