Tuesday, 3 December 2013

Code to Limit Number of Characters in MultiLine TextBox or TextArea Using jQuery In Asp.Net

Posted by Pranav Singh  |  at   Tuesday, December 03, 2013 No comments

In this article i will tell you how to limit the number of character in multiline textbox or textarea using jquery in asp.net. By using this you can limit the character in the text box.


so for this article create a new asp.net application. Add the below code in you aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Voting_system.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://code.jquery.com/jquery-1.8.3.js"></script>
    <script language="javascript">
        function ValidateCharactercount(charlimit,cont) {
            var id = "#" + cont.id;
            if ($(id).text().length > charlimit) {
                $(id).text($(id).text().substring(0, charlimit));
                $("#divmessage").html("Maximum cahracter allowes :" + charlimit);
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        MultiLine TextBox Length Validation<br />
        <asp:TextBox ID="TextBox1" runat="server" Height="95px" TextMode="MultiLine"
            Width="291px" onkeyup="javascript:ValidateCharactercount(10,this);"></asp:TextBox>
        
            <div id="divmessage" style="color:Red;"></div>
  
    </div>
    </form>
</body>
</html>

In above article i have make made 10 as a limit . In above code if you want to increase character length then just pass the desired value in the methodonkeyup="javascript:ValidateCharactercount(10,this);"

Here is the jquery code responsible for restricting the no of character.
<script language="javascript">
        function ValidateCharactercount(charlimit,cont) {
            var id = "#" + cont.id;
            if ($(id).text().length > charlimit) {
                $(id).text($(id).text().substring(0, charlimit));
                $("#divmessage").html("Maximum cahracter allowes :" + charlimit);
            }
        }
    </script>

Now run the page.



Now type the text it will not allow more then 10 character. 

About technology


Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

Related posts

0 comments :

Thanks for posting your comment

Pageviews In last 30 Days

back to top