4 December 2013

How to Create a Textarea Character Counter(or)Limiter Using jQuery?

Creating a textarea character couter using jQuery is pretty easy.

To implement this we need to write the code like as shown below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestOCRReader.WebForm1" %>

<!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></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var characterLimit = 140;
        $(document).ready(function () {
            $("#lblremaingCharacters").html(characterLimit);
            $("#meTextarea").bind("keyup", function () {
                var characterInserted = $(this).val().length;
                if (characterInserted > characterLimit) {
                    $(this).val($(this).val().substr(0, characterLimit));
                }
                var characterRemaining = characterLimit - characterInserted;
                $("#lblremaingCharacters").html(characterRemaining);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <label id="lblremaingCharacters" style="color:Red;font-weight:bold"></label><label> characters remaining.</label><br />
    <textarea id="meTextarea"></textarea>
    </div>
    </form>
</body>
</html>

Result as shown below :


No comments:

Post a Comment