2

I want alert if no file is selected. But in this code When a file is selected. It Still show The alert. Please Tell me how to fix it.

<script>
function null_upload()
{
 var a = document.getElementsByName("upload_file").value;
 if(a == null)
 {
    alert('Please Select Min 1 File.');
    return false;
 }
}
</script>

Upload Form is

<input type="file" name="upload_file">
<input type="image" src="img/upload.png"  id="upload_botton" title="Upload Image" name="submit" onclick="return null_upload()"/>
Abhijit Das
  • 107
  • 2
  • 3
  • 6

1 Answers1

7

Method getElementsByName returns a collection (i.e. a set of multiple elements) instead of a single DOM element (as, for example, getElementById does).

To get the first matched element use the following:

var a = document.getElementsByName("upload_file")[0].value;
VisioN
  • 143,310
  • 32
  • 282
  • 281