unplugged_web Posted April 15, 2014 Share Posted April 15, 2014 I've got a form that requires people to upload a photo of themselves. I'm using this to get the photo: <input type="file" name="photo" id="photo" /> and this to validate it but it's still allowing people to send the form without a photo. All of the other validation works it's just the photo part. <script type="text/javascript"> function validate_form(){ var f = document.getElementById('fname'); var l = document.getElementById('lname'); var e = document.getElementById('email'); var p = document.getElementById('phone'); var ph = document.getElementById('photo'); var fAlert = "Please enter your first name"; var lAlert = "Please enter your last name"; var eAlert = "Please enter a valid email address"; var pAlert = "Please enter a contact number"; var phAlert = "Please provide a recent photo"; var em; if (!f.value) { alert (fAlert); return false; } if (!l.value) { alert (lAlert); return false; } if (!e.value) { alert (eAlert); return false; } em = e.value.match(/[@.]/g); if ((!em) || (em.length < 2)) { alert (eAlert); return false; } if (!p.value) { alert (pAlert); return false; } if (!ph.value) { alert (phAlert); return false; } return true; } </script> Thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted April 15, 2014 Share Posted April 15, 2014 I'm not sure if you can get the value of a file input, what do you see if you print the value? Do you see any errors in the developer console? Link to comment Share on other sites More sharing options...
unplugged_web Posted April 16, 2014 Author Share Posted April 16, 2014 No I don't get any errors at all. It just submits the form regardless of whether there's a photo or not. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 16, 2014 Share Posted April 16, 2014 So what happens if you print the value? Link to comment Share on other sites More sharing options...
davej Posted April 16, 2014 Share Posted April 16, 2014 Which browser? I tried FF28 and it didn't seem to have any trouble reading the file input value. Link to comment Share on other sites More sharing options...
unplugged_web Posted April 22, 2014 Author Share Posted April 22, 2014 I'm not having any trouble getting the results it's more making sure somebody uploads a file before it's submitted. Link to comment Share on other sites More sharing options...
davej Posted April 22, 2014 Share Posted April 22, 2014 You mean -- selects a file -- before it's submitted. And as I say it looked fine in FF28. if (!ph.value) { alert ('bad file:['+ph.value+']'); return false;}else{ alert ('good file:['+ph.value+']');} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now