How can make text limiter In asp.net with java script

Step 1: Write these lines of code into Page source of default.aspx

<%@ Page Language=”C#” AutoEventWireup=”true”CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<head runat=”server”>
<script  language=”javascript” type=”text/javascript” >
var count = 12;
function limiter()
{
var tex = document.getElementById(“<%=txtComments.ClientID%>”).value;
var len = tex.length;
if (len > count)
{
tex = tex.substring(0, count)
document.getElementById(“<%=txtComments.ClientID%>”).value = tex;
return false;
}
document.getElementById(“limit”).value = count – len;
}
</script>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:TextBox ID=”txtComments” runat=”server” Height=”181px”
TextMode=”MultiLine” Width=”267px”></asp:TextBox>
<table border=”0″ cellpadding=”1″ cellspacing=”0″>
<tr>
<td><script language=”javascript” type=”text/javascript” >
document.write(“<input type=text name=’limit’ id=’limit’ size=4 readonly value=”+count+”>”);</script></td>
<td>Character Remained</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Step 2: Now Write Into page load of Page

protected void Page_Load(object sender, EventArgs e)

{

#region register client scripts

txtComments.Attributes.Add(“onkeyup”, “return limiter();”);

#endregion

}

Be happy and Enjoy in Programming World!!

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s