Home > Software engineering >  need to check input form if user upload the file or not with javascript
need to check input form if user upload the file or not with javascript

Time:11-30

i have input form to upload you file and submitting that file(method = post) i need to cheack if user upload the image or not. if not should display error message and return false. but in my case return false don't working

my code

const jobs = document.getElementById("jobImage");
const error = document.getElementById("Error");

my function

function func(){
    if(jobs.value < 1){    
     console.log("1")
     error.style.display = "block";
     error.textContent = "Pleas upload your image";
     setTimeout(function(){ window.location.reload(); },5000);
      return false
    }
        
}

when i use my function its return true not false

<div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <button  type="submit" value="Save" class="btn btn-default" id="save" onclick="func()"><a href="www.google.com">Go now</a></button>
        </div>
    </div>
</div>

best regards

CodePudding user response:

you must change if to :

if( jobs.files.length == 0 ){
    ...
}
  • Related