Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 2.17 KB | None | 0 0
  1. <==================separate .js file=================>
  2. function TextLimit() {
  3.     var maxLength = 250;
  4.     var text = document.getElementById("Textbox").value;
  5.     var length = text.length;
  6.  
  7.     document.getElementById("lblid").innerText = (((maxLength - length) < 0) ? 0 : (maxLength - length)) + " characters left";
  8.  
  9.     if (length > maxLength) {
  10.         text = text.substr(0, 250);      
  11.         alert("Limit is 250 chars");
  12.     }
  13. }
  14.  
  15. <===========.aspx file below===============================>
  16. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextLimit.aspx.cs" Inherits="limittext250.TextLimit" %>
  17.  
  18. <!DOCTYPE html>
  19.  
  20. <html>
  21. <head runat="server">
  22.     <title>Limit 250</title>
  23.     <script src="Scripts/JavaScript.js" type="text/javascript"></script>
  24.    <script>
  25.        window.onload=function() {
  26.             document.getElementById("Textbox").oninput = function () {
  27.                 TextLimit();
  28.             }
  29.         };
  30.     </script>
  31.    
  32. </head>
  33. <body>
  34.     <form id="form1" runat="server" >
  35.         <div>
  36.             Max characters allowed is 250:
  37.             <asp:TextBox ID="Textbox" runat="server" TextMode="MultiLine" Columns="50" Rows="6"  ></asp:TextBox>
  38.             <label id="lblid" runat="server" ></label>
  39.            
  40.             <%--Following is the commented javascript code that works fine when using in this same file--%>
  41.             <%--<script  type="text/javascript" >
  42.                 var maxLength = 250;
  43.                
  44.                 document.getElementById("Textbox").oninput = function () {  
  45.                     var length = document.getElementById("<%=Textbox.ClientID%>").value.length;
  46.                    
  47.                     document.getElementById("lblid").innerText = (((maxLength - length)<0)?0:(maxLength-length)) + " characters left";
  48.  
  49.                     if (length > maxLength) {    
  50.                         document.getElementById("<%=Textbox.ClientID%>").value = document.getElementById("<%=Textbox.ClientID%>").value.substr(0,250);
  51.                         alert("Limit is 250 chars");
  52.                     }
  53.                 }
  54.             </script>--%>
  55.  
  56.            
  57.         </div>
  58.     </form>
  59. </body>
  60. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement