Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Cliff on Jun 01, 2008 18:42
open dhtmlx forum
textarea limit number of characters

Hi
Using professional version 1.5
I'm using a textarea(txt) and wanted to limit the number of characters entered into this field i have tried:

if (cInd==1) {
        if (stage==1) {            
            this.obj.onpaste = function(e) {
                e = e||window.event;
                var currentLength = e.srcElement.value.length;
                var selectionLength = document.selection.createRange().text.length;
                var clipBoardLength = window.clipboardData.getData("Text").length;
                if(currentLength - selectionLength + clipBoardLength > 250) {
                    return false;
                }
            };
            this.obj.onkeypress = function(e){
                e = e||window.event;
                var currentLength = e.srcElement.value.length;
                var selectionLength = document.selection.createRange().text.length;    
                if(currentLength - selectionLength >= 250) {
                    return false;
                }
            };
        }
    }

but the onkeypress doesn't seem to fire?
thanks again
cliff
Answer posted by Support on Jun 02, 2008 02:54
The textarea, used as editor ( txt ), attached to the document.body, so all events key events go directly to the document.body and can't be catch on this.obj element.
You can use
if (cInd==1) {
        if (stage==1) {       
            this.editor.obj.onkeypress = function(e){

In such case, it will be attached directly to textarea
Answer posted by Cliff on Jun 02, 2008 18:04

OK i had to use

this._fake.editor.obj.onkeypress

and

this._fake.editor.obj.onpaste

Because the field was on the left side split in a grid.

Thanks for pointing me in the right direction.

c

Code:

 if (cInd==1) {
  if (stage==1) {   
   this._fake.editor.obj.onkeypress  = function(e){
    e = e||window.event;
    var currentLength = e.srcElement.value.length;
    var selectionLength = document.selection.createRange().text.length; 
    if(currentLength - selectionLength >= 250) {
     return false;
    }
   };
   this._fake.editor.obj.onpaste = function(e){
     e = e||window.event;
     var currentLength = e.srcElement.value.length;
     var selectionLength = document.selection.createRange().text.length;    
     if(currentLength - selectionLength >= 250) {
      return false;
     }
    };   
  }
 }