File Attachment Filter In JavaScript

. 5/26/10
  • Agregar a Technorati
  • Agregar a Del.icio.us
  • Agregar a DiggIt!
  • Agregar a Yahoo!
  • Agregar a Google
  • Agregar a Meneame
  • Agregar a Furl
  • Agregar a Reddit
  • Agregar a Magnolia
  • Agregar a Blinklist
  • Agregar a Blogmarks

HTML


<form action="" method="post" enctype="multipart/form-data">
   <input type="file" id="filelocation" name="fileField" onChange="return validateFileExtension(this)">
   <input type="reset">
   <input type="submit" value="Upload"/>
</form>




JS


function validateFileExtension(fld){
   if(!/(\.bmp|\.gif|\.jpg|\.jpeg)$/i.test(fld.value)){
      alert("Invalid image file type.");
      fld.form.reset();
      fld.focus();
      return false;
      }
      return true;
      }
  
function submitfiles(){
      alert("File location --- " +document.getElementById("filelocation").value);
      alert("File name --- " +document.forms[0].elements[0].value);
      }




Example

0 comments: