1

I select the document type from the select option then upload the file so how to validate in the code

<form name="file_up_form" method="post" name="fileupload" enctype="multipart/form-data">
    <div class="field_container1 thumbnail span3">
        <label for="select_option">Select option here</label>
        <select name="doc_type" id="select_option" style="width: 200px;" multiple>
            <option value="1">Education</option>
            <option value="2">Certification</option>
            <option value="3">Client Paperwork</option>
            <option value="4">Invoice</option>
        </select>
    </div>
    <div class="field_container thumbnail span4">
        <label for="file">Upload here: </label>
        <input type="file" name="file" id="file">
        <input class="btn btn-primary" type="submit" value="submit" name="submit">
    </div>
    <input type="hidden" name="check" value="1">
</form>

Here is the validation code I'm using so far:

<script> 
    function validate() 
    { 
        if(document.fileupload.doc_type.value == "" && document.fileupload.doc_type.value == null) 
        { 
            alert( "Please provide your document!" ); 
            return false; 
        } 
        if(document.fileupload.file.value == "" && document.fileupload.file.value == null) 
        {   
            alert( "Please provide your file!" ); 
            return false; 
        } 
        return true; 
    } 
</script>
3
  • Have you tried anything of your own? Have you looked at other modules to see how they do it? I Assume you have a helper.php file so you can detect the forum submission and add you validation in there
    – Lodder
    Jun 19, 2014 at 8:26
  • Thanks your response friend.i using normal javascript code this code working but con't raise alert message?..<script> function validate() { if(document.fileupload.doc_type.value == "" && document.fileupload.doc_type.value == null) { alert( "Please provide your document!" ); return false; } if(document.fileupload.file.value == "" && document.fileupload.file.value == null) { alert( "Please provide your file!" ); return false; } return true; } </script>
    – reegan29
    Jun 19, 2014 at 8:29
  • Remove return false; from your code. Also make sure you use the browser console to check for any error that may be given
    – Lodder
    Jun 19, 2014 at 8:36

1 Answer 1

3

Change document.fileupload.doc_type.value to document.getElementById('file').value. Also, you're checking if the value is "" AND null, I'm not sure if that is possible.

Unless you need to verify the file format etc., try something simpler like this:

HTML

<form onsubmit="return(validate());" name="file_up_form" method="post" name="fileupload" enctype="multipart/form-data" >
    <div class="field_container1 thumbnail span3">
        <label for="select_option">Select option here</label>
        <select name="doc_type" id="select_option" style="width: 200px;" multiple>
            <option value="1">Education</option>
            <option value="2">Certification</option>
            <option value="3">Client Paperwork</option>
            <option value="4">Invoice</option>
        </select>
    </div>
    <div class="field_container thumbnail span4">
        <label for="file">Upload here: </label>
        <input type="file" name="file" id="file">
        <input class="btn btn-primary" type="submit" value="submit" name="submit">
    </div>
    <input type="hidden" name="check" value="1">
</form>

JavaScript

<script> 
    function validate() 
    { 
        if(!document.getElementById('file').value) 
        { 
            alert( "Please provide your document!" ); 
        } 
        return false; 
    } 
</script>

Shouldn't you also return false (not true as you currently do) if no file is selected to stop the form from being submitted? The <form> tag should probably include an action parameter too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.