Search code examples
javascripttextareanicedit

nicEdit textarea name to do checking


I want to do checking with my textarea where if the user does not fill in the message, it will prompt out an alert message. However my textarea seem like it is replaced by a nicEdit textarea.

HTML part

<head>
<script type="text/javascript" src="/ecover/common/editor/editor.js"></script> 
<script type="text/javascript">
bkLib.onDomLoaded(function() {new nicEditor({fullPanel : true}).panelInstance('MESSAGE');});
function validate()
{ 
 if(document.mainfrm.MESSAGE.value=="")
   { 
     alert("Please enter the value Message");
     document.mainfrm.MESSAGE.focus();
     return false;
   }
    return true;
}
</script>
</head>
<body> 
  <form name="mainfrm" method="post" action="addMessageRep.jsp" onSubmit="return validate();">   
    <textarea id="MESSAGE" name="MESSAGE" cols="80" rows="20" ></textarea>
  </form> 
</body>

nicEdit.js part affected area

var isTextarea = (e.nodeName.toLowerCase() == "textarea");
    if(isTextarea || this.options.hasPanel) {

        var ie7s = (bkLib.isMSIE && !((typeof document.body.style.maxHeight != "undefined") && document.compatMode == "CSS1Compat"))
        var s = {width: newX+'px', border : '1px solid #ccc', borderTop : 0, overflowY : 'auto', overflowX: 'hidden' };
        s[(ie7s) ? 'height' : 'maxHeight'] = (this.ne.options.maxHeight) ? this.ne.options.maxHeight+'px' : null;
        this.editorContain = new bkElement('DIV').setStyle(s).appendBefore(e);
        var editorElm = new bkElement('DIV').setStyle({width : (newX-8)+'px', margin: '4px', minHeight : newY+'px'}).addClass('main').appendTo(this.editorContain);

        e.setStyle({display : 'none'});

        editorElm.innerHTML = e.innerHTML;      
        if(isTextarea) {
            editorElm.setContent(e.value);
            this.copyElm = e;
            var f = e.parentTag('FORM');
            if(f) { bkLib.addEvent( f, 'submit', this.saveContent.closure(this)); }
        }
        editorElm.setStyle((ie7s) ? {height : newY+'px'} : {overflow: 'hidden'});
        this.elm = editorElm;   
    }

Step for testing:

  1. First attempt when I fill in "test" value in textarea and click on save button, it will prompt "please enter the value message"
  2. After that close the message box and click again save button, the value is saved. Supposedly if I fill in value in textarea it should save the value instead of prompt message.

Solution

  • to get value of niceEditor textarea, try doing:

    var nicInstance = nicEditors.findEditor('MESSAGE');
    var messageContent = nicInstance.getContent();
    //then check for messageContent
    

    Update::

    bkLib.onDomLoaded(function() {new nicEditor({fullPanel : true}).panelInstance('MESSAGE');});
    function validate()
    { 
        var nicInstance = nicEditors.findEditor('MESSAGE');
        var messageContent = nicInstance.getContent();
        //since nicEditor sets default value of textarea as <br>
        //we are checking for it
        if(messageContent=="<br>") { 
         alert("Please enter the value Message");
         document.mainfrm.MESSAGE.focus();
         return false;
       }
       else {
            alert("valid");
       }   
        return true;
    }
    

    Demo:: jsFiddle