Prior to submitting a form, I would like to check if a file has been attached and pop up a warning message saying that a file needs to be attached if it hasn't been. I was wondering how to acplish this using JavaScript or Prototype or JQuery etc?
Prior to submitting a form, I would like to check if a file has been attached and pop up a warning message saying that a file needs to be attached if it hasn't been. I was wondering how to acplish this using JavaScript or Prototype or JQuery etc?
Share Improve this question edited Dec 29, 2011 at 15:01 Rob W 349k87 gold badges807 silver badges682 bronze badges asked May 10, 2010 at 15:41 pvsk10pvsk10 2613 silver badges6 bronze badges1 Answer
Reset to default 15Assuming you are using an <input type="file">
field, you can simply check if the element's value is a non-empty string:
<form method="POST">
<input type="file" id="attachment" />
<input type="button" onClick="checkAttachment();" value="Send" />
</form>
<script type="text/javascript">
function checkAttachment() {
if (document.getElementById('attachment').value !== '') {
alert('File Attached');
}
else {
alert('No File Attached');
}
}
</script>