Javascript空字段未正确验证

时间:2013-12-17 13:03:37

标签: javascript ckeditor

好吧,我有一个通过javascript验证的html表单。在此表单中有一个编辑器textarea 框。当此框为空时,javascript警告消息显示“页面内容需要”。没关系。

但是在填满内容后,它再次向我显示“页面内容要求”。我的代码有什么问题?

使用Javascript:

function doStart()
    {       
    var page_name =  document.page.page_name.value; 
    var page_loc = document.page.page_location.value;   

    <?php

    $page_limit =  mysql_query("SELECT menu_name FROM cms WHERE username = '$username' AND cms_location = 1 ");
    $num_page_limite = mysql_num_rows($page_limit);
    ?>
    var num = <?php echo $num_page_limite; ?>


    var page_content = document.page.editor_kama.value; 
    var uploadobj = document.getElementById('myuploader');


    if(page_name == null || page_name == "")
    {
        alert("page name require"); 
        document.page.page_name.focus() ;
        return false;
    }
    else if(page_name.length > 15 )
    {
        alert("page name is too long");
        document.page.page_name.focus() ;
        return false;
    }

    if(page_loc == null || page_loc == "")
    {
        alert("Select page location");  
        document.page.page_location.focus() ;       
        return false;
    }
    else if(num == 5 && page_loc == 1)
    {
        alert("You already creaed 5 pages for your top menu.");
        return false;
    }

    if(page_content == null|| page_content == "")       
    {
        alert("Page content require");  
        return false;
    }

    if (uploadobj.getqueuecount() > 0)
    {
        uploadobj.startupload();
    }
    else
    {
        alert("Please browse files for upload");
    }

}
</script>

HTML表单:

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" id="form1"  name="page" onSubmit="return validate()">
    <table width="100%" border="0" cellspacing="10" cellpadding="0" style="float:left; position:relative;">           
    <tr>
    <td valign="top">Page Content</td>
    <td>
    <textarea cols="80" id="editor_kama" name="editor_kama" rows="30" class="textarea"><?php if(isset($_POST['editor_kama'])) echo $_POST['editor_kama'];?></textarea>
     <script type="text/javascript">
       //<![CDATA[

       CKEDITOR.replace( 'editor_kama', {
           skin : 'kama'
       });

       //]]>
    </script>
    </td>
    </tr> 
    <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Create page" class="submit" id="submitbutton" onclick="doStart();return false;"/></td>
    </tr>
    </table>            
</form>

1 个答案:

答案 0 :(得分:2)

解决方案是

var page_content = document.page.editor_kama.value;

通过

var page_content = CKEDITOR.instances['editor_kama'].getData();

您的编辑器会生成大量代码,您必须使用文档来与它们进行交互。

编辑

page_content == null|| page_content == ""替换为page_content.length == 0