How to validate multiple input type=file with Javascript
Hello,
I have a form which can contain dynamically 1 or a few input type=file fieldsm and I want to write a javascript validation which will tell the user to submit files in each field, if its blank, before submitting the form. I need this validation done on the client-side.
Heres my non-working example, i think its only a syntax problem...
<script language="javascript" type="application/javascript">
function verify(file) {
var f = document.my_form.file;
if (f.value == '')
{
alert("Please choose a file.");
return false;
}
}
</script>
****************************************************
ultimately, the best function would be verifier('2') where ('2') would be the total number of input type=file in the form, then a "for" loop would go and check each of them. but i guess this would require to name each input as file[0], file[1], which is not a problem...
I found this which seems to do the trick, can someone advise if ok?
<script language="javascript" type="application/javascript">
function verif() {
var node_list = document.getElementsByTagName('input');
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
if (node.getAttribute('type') == 'file') {
if (node.value == '') {
alert("Please choose a file");
return false;
}
}
}
}
</script>
FF doesnt give me any visible errors, havent tested IE yet...
i spent at least 1 hour searching for an easy script to validate only a dynamic umber of <input type="file" ...> and this seemed to be the best solution.
so the script works PERFECTLY in FF, but NOTHING HAPPENS in IE...
can someone help me PLEASE?
<script language="javascript" type="application/javascript">
function verif() {
var node_list = document.getElementsByTagName('input');
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
if (node.getAttribute('type') == 'file') {
if (node.value == '') {
alert("Please choose a file");
return false;
}
}
}
}
</script>
i've never liked this browser incompatibility issue... everything to make our lives tougher! ARGH!
Bookmarks