Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <==================separate .js file=================>
- function TextLimit() {
- var maxLength = 250;
- var text = document.getElementById("Textbox").value;
- var length = text.length;
- document.getElementById("lblid").innerText = (((maxLength - length) < 0) ? 0 : (maxLength - length)) + " characters left";
- if (length > maxLength) {
- text = text.substr(0, 250);
- alert("Limit is 250 chars");
- }
- }
- <===========.aspx file below===============================>
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextLimit.aspx.cs" Inherits="limittext250.TextLimit" %>
- <!DOCTYPE html>
- <html>
- <head runat="server">
- <title>Limit 250</title>
- <script src="Scripts/JavaScript.js" type="text/javascript"></script>
- <script>
- window.onload=function() {
- document.getElementById("Textbox").oninput = function () {
- TextLimit();
- }
- };
- </script>
- </head>
- <body>
- <form id="form1" runat="server" >
- <div>
- Max characters allowed is 250:
- <asp:TextBox ID="Textbox" runat="server" TextMode="MultiLine" Columns="50" Rows="6" ></asp:TextBox>
- <label id="lblid" runat="server" ></label>
- <%--Following is the commented javascript code that works fine when using in this same file--%>
- <%--<script type="text/javascript" >
- var maxLength = 250;
- document.getElementById("Textbox").oninput = function () {
- var length = document.getElementById("<%=Textbox.ClientID%>").value.length;
- document.getElementById("lblid").innerText = (((maxLength - length)<0)?0:(maxLength-length)) + " characters left";
- if (length > maxLength) {
- document.getElementById("<%=Textbox.ClientID%>").value = document.getElementById("<%=Textbox.ClientID%>").value.substr(0,250);
- alert("Limit is 250 chars");
- }
- }
- </script>--%>
- </div>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement