Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*First you need this in your html */
- <fieldset class="images">
- <input type="file" name="imagenupload1" id="imagenupload1" tabindex="25" />
- </fieldset>
- /*Now in your javascript file*/
- /*First we define some variables*/
- var validar_imagenupload=new Boolean(false);
- var validar_imagenextension=new Boolean(false);
- /* This are the valid extensions. Add or remove as you wish */
- var extensiones_permitidas = new Array(".gif", ".jpg", ".jpeg", ".png");
- /* Now, the image upload validation */
- $('#imagenupload1').change(function(){
- validar_imagenupload=false;
- validar_imagenextension = false;
- var node = document.getElementById('imagenupload1');
- var check = node.files[0].fileSize;
- var nombreImg = node.files[0].fileName;
- var extension = (nombreImg.substring(nombreImg.lastIndexOf("."))).toLowerCase();
- /* check for maximum size. In this case is 300KB. You can set to whatever you want */
- if (check < 300001){
- //Size is OK
- /* Check file extension */
- for (var i = 0; i < extensiones_permitidas.length; i++) {
- if (extensiones_permitidas[i] == extension) {
- validar_imagenupload = true;
- validar_imagenextension = true;
- break;
- } else{
- validar_imagenextension = false;
- }
- }
- } else{
- alert('File is too big');
- clearFileUpload('imagenupload1');
- validar_imagenextension = true;
- }
- if (validar_imagenextension == false){
- alert('File is not an image');
- clearFileUpload('imagenupload1');
- }
- });
- /* We also need this function */
- function clearFileUpload(id){
- // get the file upload element
- fileField = document.getElementById(id);
- // get the file upload parent element
- parentNod = fileField.parentNode;
- // create new element
- tmpForm = document.createElement("form");
- parentNod.replaceChild(tmpForm,fileField);
- tmpForm.appendChild(fileField);
- tmpForm.reset();
- parentNod.replaceChild(fileField,tmpForm);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement