1

I am using multipart to upload a file; I want to prevent the form from being submitted if the file has not been selected.

How can I achieve this ? This is my code:

<form id="uploadFile" name="uploadFileForm" action="<s:url value="/admin/saveExcelDatarestore" />" method="post" enctype="multipart/form-data"><span>
    <input type="hidden" name="s360securetoken" value="<s:property value="#session.s360securetoken"/>"/>
    <s:hidden name="user.userId"></s:hidden>
    <s:hidden name="user.emailId"></s:hidden>
    <div><label>DownLoad Sample :</label><input type="button" class="submit" value="XLS" onclick="dnldSampleData('XLS');"></span></div>
 <div><label>Upload File :</label> <input type="file" name="upload" value="" id="uploadFile_upload"></span>

<span><select id="selectedTimeZone" name="timeZone" style="width:100%;white-space: nowrap; overflow: hidden;" class="chosen-select1">
 <jsp:include page="../../../../timezone.jsp"/> 
    </select></span><div>
<input type="submit" value="upload" class="submit" onclick="upload_file();"></form>

Here if I do not select file it's also redirecting tme to saveExecelDatarstore, but I want that if there is no file selected then it shoud not show any message, and return back

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Shakti Sharma
  • 2,131
  • 3
  • 18
  • 23

3 Answers3

2

You can write a code in java-script on click of the submit button

function upload_file()
{
        e.preventdefault();
        var file = document.getElementById("uploadFile_upload");
        if(file.value!=""){
         $("#uploadFile").submit()
         alert("file selected");
        }else{
        alert("nothing selected");
         return false;
        }
}
0

Assuming upload_file() is performing a check on the presence of the file (otherwise, it's not clear what it does, since it's applied to a submit button), you need to return its result. If it's false, the submit will be aborted:

<input type="submit" value="upload" class="submit" onclick="return upload_file();">

<script>
    function upload_file(){
        return document.getElementById("uploadFile_upload").files.length > 0;
    }
<script>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
0

First make your input tag's onclick take an event as paramter to upload_file function (event and this are spacial objects passed to function and you can use them inside a function without declaring them in the function header):

<input type="submit" value="upload" class="submit" onclick="upload_file(event);"></form>

then define the upload_file as following:

function upload_file(e)
{
        e.preventdefault(); // or event.preventdefault();
        var file = document.getElementById("uploadFile_upload");
        if(file.value!=""){
            // get form element through id:
            document.getElementById("uploadFile").submit();
        } 
        return false;           
}

Read more:

onClick Function “this” Returns Window Object

How to get the onclick calling object?

onclick assigned function with parameters

How to submit a form with JavaScript by clicking a link?

By the way you can use a good jquery form plugin for sending an ajax form request containing file upload too, It help me a lot in the past with struts2 : http://malsup.com/jquery/form/#file-upload

Community
  • 1
  • 1
mohamed sulibi
  • 526
  • 3
  • 12