Re: How to display javascript alert for exceeding text length?

  •  01-20-2011, 8:34 PM

    Re: How to display javascript alert for exceeding text length?

    Hi karmyogi,
     
    Please try the example below. Limit 5 characters.
     
    1. <%@ Page Language="C#" %>  
    2.   
    3. <%@ Register Assembly="CuteEditor" Namespace="CuteEditor" TagPrefix="CE" %>  
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    5. <html xmlns="http://www.w3.org/1999/xhtml">  
    6. <head id="Head1" runat="server">  
    7.     <title>GetPlainText</title>  
    8. </head>  
    9. <body>  
    10.     <form id="form1" runat="server">  
    11.         <CE:Editor ID="editor1" runat="server" ShowBottomBar="false">  
    12.         </CE:Editor>  
    13.         <div id="plainText" style="visibility: hidden">  
    14.         </div>  
    15.     </form>  
    16. </body>  
    17. </html>  
    18.   
    19. <script>  
    20. function isIE() {  
    21.     if (window.navigator.userAgent.toLowerCase().indexOf("msie") != -1) return true;  
    22.     else return false;  
    23. }  
    24.   
    25. function GetTextLenght() {  
    26.   
    27.     var editor1 = document.getElementById('<%= editor1.ClientID %>');  
    28.     var plainText = document.getElementById("plainText");  
    29.     plainText.innerHTML = editor1.GetHTML();  
    30.     var text;  
    31.     if (isIE()) {  
    32.         text = plainText.innerText;  
    33.     }  
    34.     else {  
    35.         text = plainText.textContent;  
    36.     }  
    37.     plainText.innerHTML = "";  
    38.     return text.length;  
    39. }  
    40.   
    41. function disableEvent(oEvent)  
    42. {  
    43.     if(GetTextLenght()>=5)  
    44.     {  
    45.         //alert("characters more than 5");  
    46.         oEvent.preventDefault();     
    47.     }  
    48.      
    49. }  
    50. function CuteEditor_OnInitialized(editor)  
    51. {  
    52.     var editdoc = editor.GetDocument();  
    53.     if(window.addEventListener)  
    54.     {  
    55.           
    56.         editdoc.addEventListener("keypress",disableEvent,"false");  
    57.     }  
    58.      
    59.     editdoc.body.onkeydown=function()  
    60.     {  
    61.         if(GetTextLenght()>=5)  
    62.         {  
    63.             return false;  
    64.         }  
    65.          
    66.     }  
    67. }  
    68. </script> 
     
    Regards,
     
    Ken
View Complete Thread