Skip to content

Instantly share code, notes, and snippets.

@orafaelfragoso
Created May 14, 2013 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orafaelfragoso/5580446 to your computer and use it in GitHub Desktop.
Save orafaelfragoso/5580446 to your computer and use it in GitHub Desktop.
jQuery code to validate input fields
<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>
$("#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