Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Validate input type file

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #146526
    kanshul2013
    Participant

    I have a html file input control for selecting file. When form is submitted I need to check if user actually selected something, if not show an alert and do not submit.

    Following is the HTML code

    <form name="" id="" ....>
    <input type="file" name="zip_file_import" id="zip_file_import" />
    <input type="button" value="Upload" onclick="formSubmit()"  />
    </form>
    

    JavaScript is as follows

    function formSubmit() {
       if(document.getElementById("zip_file_import").value='') {
    alert("Please select a zip file");
    return false;
       }
    

    document.getElementById(“import_form”).submit();
    document.body.style.cursor = “wait”;
    }

    This code does not work and the form submits even no file is selected by the user. Any idea what could be wrong?

    Thank you

    #146565
    g3logic
    Participant

    The syntax is wrong in your JS. You are setting a value to document.getElementById(“zip_file_import”).value, instead of comparing its value as intended.

    Use double equal sign and you should be good to go:

    if(document.getElementById("zip_file_import").value == ""){
    
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.