Alex
Alex

Reputation: 97

Javascript empty field is not properly validate

Well, I've a html form which validate by javascript. In this form there a editor textarea box. When this box is empty javascript alert message showing "page content require". That's Ok.

But after filled up the content then it's again showing me "page content require". What's is wrong in my code ?

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:

<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>

Upvotes: 0

Views: 3910

Answers (1)

kevpoccs
kevpoccs

Reputation: 635

The solution is

var page_content = document.page.editor_kama.value;

by

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

your editor generate a lot of code, you must use the documentation to interract with them.

edit :

replace page_content == null|| page_content == "" by page_content.length == 0

Upvotes: 2

Related Questions