Ver Mensaje Individual
  #3 (permalink)  
Antiguo 17/01/2011, 05:50
Avatar de angel_dope
angel_dope
 
Fecha de Ingreso: noviembre-2002
Ubicación: Valencia
Mensajes: 737
Antigüedad: 21 años, 5 meses
Puntos: 8
Respuesta: Controlar extensión en un input de tipo file antes de enviar formulario

Ya lo he resuelto, os dejo el resultado, por si a alguien le hace falta ;)

Código Javascript:
Ver original
  1. <script language="javascript">
  2. function Right(str, n)
  3.         /***
  4.                 IN: str - the string we are RIGHTing
  5.                     n - the number of characters we want to return
  6.  
  7.                 RETVAL: n characters from the right side of the string
  8.         ***/
  9.         {
  10.                 if (n <= 0)     // Invalid bound, return blank string
  11.                    return "";
  12.                 else if (n > String(str).length)   // Invalid bound, return
  13.                    return str;                     // entire string
  14.                 else { // Valid bound, return appropriate substring
  15.                    var iLen = String(str).length;
  16.                    return String(str).substring(iLen, iLen - n);
  17.                 }
  18.         }
  19. </script>  
  20.  
  21. <script language="javascript">
  22. function validar(frm) {
  23.  var ret;
  24.  var txt;
  25.  var txt2;
  26.  
  27.  txt = Right(frm.fichero.value,3);
  28.   txt2 = Right(frm.fichero2.value,3);
  29.  if (txt != "pdf"){
  30.   alert('Por favor, el primer archivo debe ser .pdf');
  31.   ret = false;
  32.  }
  33.  if (txt2 != "doc"){
  34.   alert('Por favor, el segundo archivo debe ser .doc');
  35.   ret = false;
  36.  }
  37.  if (frm.firma.value == "" ) {
  38.   alert('Por favor, seleccione si quiere firmar el informe o no');
  39.   ret = false;
  40.  }
  41. return ret;
  42. }
  43. </script>

La primera función saca N caracteres de una cadena dada. A partir de ahí saco los tres últimos de ambos campos file y compruebo que sean pdf y doc respectivamente. Si se cumple, sigue adelante, sino, avisa al usuario ;)
__________________
Vayamos por Partes :: Jack el Destripador