Created
October 18, 2012 22:15
-
-
Save mildfuzz/3915097 to your computer and use it in GitHub Desktop.
validateImageUpload
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
| $.fn.validateImageUpload = function(size){ | |
| //Accepts form file input fields $('input[type=file]').validateImageUpload(); | |
| //Also accepts size validation (in K), default is 700. Set to false in order to skip check | |
| if(typeof size != "number" && size%1 != 0){size = 700} | |
| var validate = function(x){ | |
| var files = $(x).get(0).files, that = x; | |
| for (i = 0; i < files.length; i++){ | |
| if(!files[i].type.match(/(gif|jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG)/)){ | |
| alert('Must be an image file.'); | |
| $(that).val(''); | |
| return false; | |
| } | |
| if(size && files[i].size > size*1000){ | |
| alert('File be an under '+size+'k.'); | |
| $(that).val(''); | |
| return false; | |
| } | |
| return true; | |
| } | |
| } | |
| $('body').on('change',$(this).selector,function(){ | |
| if(validate(this)){ | |
| return true; | |
| } else { | |
| $(this).val(""); | |
| return false; | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment