Created
May 14, 2013 23:10
-
-
Save orafaelfragoso/5580446 to your computer and use it in GitHub Desktop.
jQuery code to validate input fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="uploadImages"> | |
<input type="file" id="fotos" multiple="true" /><br /> | |
<p class="callback">Try to upload some files here.</p> | |
<input type="submit" name="Submit" /> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#uploadImages").on('submit', function(){ | |
var photos = $('#fotos'); | |
var p = $('.callback'); | |
var allowed_types = new Array( | |
"application/pdf", | |
"image/jpeg", | |
"image/png" | |
); | |
// Just so you can check out the file object in the console | |
console.log(photos); | |
// Don't send it blank | |
if(photos[0].files.length == 0) { | |
p.html("You should upload at least 1 file."); | |
return false; | |
} | |
// http://www.feedforall.com/mime-types.htm | |
$.each(photos[0].files, function(index, file){ | |
// validates extensions | |
if( allowed_types.indexOf(file.type) !== 0 ) { | |
p.html("You can only upload: pdf, jpg and png."); | |
return false; | |
} | |
// validates size | |
if( file.size > 1048576 ) { | |
p.html("The max filesize is 1MB."); | |
return false; | |
} | |
}); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment