Tuesday, February 16, 2010

Limit characters in a long edit box

< script language="text/javascript" >
   //get the outer html
   if(document.getElementById("YOUR_REC_YOUR_FLD$0")!=null)
   {var outerhtm=document.getElementById("YOUR_REC_YOUR_FLD$0").outerHTML;
    //declare our function call
    var addedText= " onchange=this.focus; onkeyup=this.focus; onblur=parseText(this); "
    // reconstruct the outer htm
    outerhtm=outerhtm.substring(0,outerhtm.indexOf(">"))+addedText+ outerhtm.substring(outerhtm.indexOf(">"),outerhtm.length);
    //assign the reconstructed htm.
   document.getElementById("YOUR_REC_YOUR_FLD$0").outerHTML=outerhtm;
   }
   
   function parseText(textElem)
   {//this function does the length checking for objects defined in the case statements below. 
   switch(textElem.name)
    {
    case "YOUR_REC_YOUR_FLD$0":
     if(textElem.value.length >650 ){alert("Short Description has a maximum length of 650 characters\nIt is currently "+textElem.value.length+" characters.");textElem.style.background="red";textElem.focus();}
     else{textElem.style.background="white";}
    break;
    }
   }
< /script >

No comments:

Post a Comment