How to count remaining character in the textbox using javascript in asp.net c#


In this article, we will count the remaining character in textbox using java script.

Java Script Function:-
    <script type="text/javascript">


        function charcounter(txtid, counttxtid, maxcharlimit) {

            if (txtid.value.length > maxcharlimit)
                txtid.value = txtid.value.substring(0, maxcharlimit);
            else
                counttxtid.value = maxcharlimit - txtid.value.length;
        }

    </script>


Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="charcounter.aspx.cs" Inherits="charcounter" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> Characters Counter </title>
    <script type="text/javascript">

        function charcounter(txtid, counttxtid, maxcharlimit) {

            if (txtid.value.length > maxcharlimit)
                txtid.value = txtid.value.substring(0, maxcharlimit);
            else
                counttxtid.value = maxcharlimit - txtid.value.length;
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <fieldset style="width: 280px;">
            <legend>Counting Characters </legend>
            <asp:TextBox ID="txtbox" Width="280px" runat="server" TextMode="MultiLine" onkeyup="charcounter(txtbox, this.form.counter, 150);"
                onkeydown="charcounter(txtbox, this.form.counter, 150);" Height="51px" />
            <input type="text" name="counter" size="3" maxlength="3" value="150" readonly="readonly" />
            Remaining Characters
        </fieldset>
    </div>
    </form>
</body>
</html>

Out Put:-


 
How to count remaining character in the textbox using javascript in asp.net c# How to count remaining character in the textbox using javascript in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 3:11:00 PM Rating: 5

No comments:

Powered by Blogger.