1 / 4
2 / 4
3 / 4
4 / 4

Wednesday, 5 February 2020

Text counter/Character counter in Asp .Net and JQuery with Demo

In this post, I am going to show you the simple JQuery code for counting the Text/Character that is entered in the TextBox. And after reaching the limit it will stop the user to enter any Text.
Demo Video is at the bottom of this post

In the <head> tag:- 


<head runat="server">
    <title>TextCounter</title>
</head>

In the <form> tag:-

<form id="form1" runat="server">
        <div>
            <h2>Message :</h2>
            <asp:TextBox ID="txtMessage" Height="150px" Width="250px" TextMode="MultiLine" onkeyup="textCount(this)" runat="server" />
            &nbsp;&nbsp;(<asp:Label ID="lblCountmsg" ForeColor="#0066ff" Text="150" runat="server" />/150)
        </div>
    </form>

Add the JQuery Script within <body> tag:- 

    <script>
        function textCount(val) {
            var len = val.value.length;
            if (len > 150) {
                val.value = val.value.substring(0, 150);
                e.preventDefault();
            }
            else {
                $('#lblCountmsg').text(150 - len);
            }
        
        };
    </script>


Overview of my vs code:- 





Demo video:-



No comments:

Post a Comment

If you have any doubts please let me know