javascript - How to return back and do not submit form -


i using multipart upload file; want prevent form being submitted if file has not been selected.

how can achieve ? 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 not select file it's redirecting tme saveexeceldatarstore, want if there no file selected shoud not show message, , return back

you can write code in java-script on click of 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;         } } 

Comments