This is a migrated thread and some comments may be shown as answers.

Character Counter on HTML Source side of Editor

2 Answers 192 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 01 Apr 2009, 05:28 PM
I have an app that I need to limit the amount of characters that can be entered by a customer but I giving them the editor to enter them in WYSIWYG form.  I need to be able to give them a counter of the html characters not the typed characters realtime while typing.  Can I do that with this module?

2 Answers, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 06 Apr 2009, 07:17 AM
Hi Andrew,

Please, see the following Validator support example which demonstrates how to attach a custom validator to RadEditor and restrict the content length.

Another approach is to use the following code:

<script type="text/javascript"
var maxTextLength = 30; 
var messageText = 'You are not able to type more than ' + maxTextLength + ' symbols!';   
    
 function isAlphaNumericKey(keyCode)   
 {   
      if ((keyCode > 47 && keyCode < 58) || (keyCode > 64 && keyCode < 91))   
      {   
          return true;   
      }   
      return false;   
 }   
    
 function LimitCharacters(editor)   
 {   
     editor.attachEventHandler ("keydown"function (e)   
     {   
         e = (e == null)? window.event : e;   
         if (isAlphaNumericKey(e.keyCode))   
         {   
             textLength = editor.get_text().length;   
             if (textLength >= maxTextLength)   
             {   
                  alert(messageText);   
                  e.returnValue = false;   
                  return false;   
             }   
         }   
      });   
 }   
 
 function CalculateLength(editor, value) 
 { 
     var textLength = editor.get_text().length; 
     var clipboardLength = value.length; 
     textLength += clipboardLength; 
     return textLength; 
 } 
 
 function OnClientPasteHtml(editor, args) 
 { 
    var commandName = args.get_commandName();    
    var value = args.get_value(); 
     
        if (commandName == "PasteFromWord" 
            || commandName == "PasteFromWordNoFontsNoSizes" 
            || commandName == "PastePlainText" 
            || commandName == "PasteAsHtml" 
            || commandName == "Paste" ) 
        { 
            var textLength = CalculateLength (editor, value); 
            if (textLength >= maxTextLength) 
            { 
              alert("You are not allowed to enter more that 30 symbols"); 
              args.set_cancel(true); 
 
            } 
        }  
 } 
</script> 
<telerik:RadEditor id="RadEditor1" OnClientPasteHtml="OnClientPasteHtml" OnClientLoad="LimitCharacters" 
    Runat="server"
    <Content> 
</Content> 
</telerik:RadEditor>  


Regards,
Rumen
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Jeremy Coenen
Top achievements
Rank 1
answered on 25 Jun 2009, 03:29 PM
There appears to be flaws in this code as it does not count a number of characters that I think should be checked.  For example all of the number pad numbers (0-9) are ignored, as well as period,comma, and a host of other relevant characters.  Below is my modified version of isAlphaNumericKey

 function isAlphaNumericKey(keyCode) { 
      if ( 
          (keyCode > 47 && keyCode < 58) || 
          (keyCode > 64 && keyCode < 91) || 
          (keyCode > 93 && keyCode < 112) || 
          (keyCode > 185 && keyCode < 193) || 
          (keyCode > 218 && keyCode < 223) 
         )  
          { 
            return true
          } 
      return false
    } 

Tags
Editor
Asked by
Andrew
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Jeremy Coenen
Top achievements
Rank 1
Share this question
or