hace poco que he hecho una validacion de un formulario con ficheros:
Javascript
:
Código:
// funcion para validar el formulario
function validate(hau)
{
var extension_allow=new Array('mp3','wma','avi','mpg','mpeg','mov','3gp','mp4','wmv','flv','jpg','jpeg','gif'); // extensiones validas
var valid=true;
var inputs=hau.getElementsByTagName('input');
for(var i=0;i<inputs.length;i++)
{
var type=inputs[i].type;
var value=inputs[i].value;
if(type=='text')
{
if(trim(value)=='')
{
alert('Titulo es un campo obligatorio');
valid=false;
}
}
else if(type=='file')
{
if(trim(value)=='')
{
alert('Es obligatorio adjuntar un archivo');
valid=false;
}
else
{
var ext_position=value.lastIndexOf('.'); // posicion de la extension
if(ext_position==-1)
{
alert('Extension no valida');
valid=false;
}
else
{
ext_position++;
var ext_length=value.length-ext_position;
var ext=value.substr(ext_position,ext_length).toLowerCase();
if(in_array(ext,extension_allow,true)==false)
{
alert("Extension no valida: "+ext);
valid=false;
}
}
}
}
}
return valid;
}
// borra espacios por la derecha y por la izquierda
function trim(text)
{
text=text.replace(/^\s+|\st+$/g,'');
return text;
}
// busca texto en un array
function in_array(needle, haystack, strict)
{
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
// * returns 1: true
var found = false, key, strict = !!strict;
for (key in haystack)
{
if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle))
{
found = true;
break;
}
}
return found;
}
HTML
Código HTML:
<form action="" method="post" onsubmit="return validate(this)" enctype="multipart/form-data">
<input name="titulo" type="text" />
<input name="file" type="file" />
<input type="submit" value="enviar" />
</form>
Tambien se puede hacer, ponerle en la ID al final un * para que al recorrer todos los campos por javascript filtrarlos por ese * para saber si es obligatorio o no, pero bueno eso te lo dejo a ti.
Aqui solo hay ejemplos para tipo texto y tipo fichero, espero que te sirva ;)