PDA

View Full Version : javascript empty file upload field check



insei
20 Jun 2010, 09:33 AM
hi

is there a way i can check if a file upload field is empty or not with javascript/jquery when the submit button is pressed?

Asperon
20 Jun 2010, 02:45 PM
This is the general idea, though, I'm pretty sure you can't target a file upload like that. YOu might have to change the type value to a textbox, and then get the value out of there.


<html>
<body>
<form id="form" onsubmit="checkForm(event, this;">

Upload File: <input id="fileUpload" type="file" name="file1" value="C:\" />
<br />
<input type="submit" value="Submit" />
</form>

<script type="text/javascript">
function checkForm(e, t){
if(e.stopPropagation){
e.stopPropagation();
}
else{
e.cancelBubble = true;
}

if (e.preventDefault){
e.preventDefault();
}
else{
e.returnValue = false;
}
if(!document.getElementById("fileUpload").value){
alert("No file selected");
}
else{
t.submit();
}

}
</script>
</body>
</html>

or you can do:


$('#lform').submit(function(e) {
stopEvent(e);
if you use jquery ( I don't so it might not be accurate)