1

I have a JQuery script that validates the upload of avatar images but I need it to prevent the upload of anything other than PNG, JPG & GIF images. Any way of implementing this into the code I have? Here is the code:

$('#addButton').click(function () {
    var avatar = $("#avatarupload").val();
    if(avatar.length < 1) {
        avatarok = 0;
    }
    //ELSE IF FILE TYPE
    else {
        avatarok = 1;
    }
    if(avatarok == 1) {
        $('.formValidation').addClass("sending");
        $("#form").submit();
    }
    else {
        $('.formValidation').addClass("validationError");
    }
    return false;
});
4
  • Isn't there a way of checking the file name for the extension? Like, if(avatar.name includes jpg){} sort of thing? Commented Jan 6, 2014 at 12:52
  • stuff like this can be easily manipulated and create lot of unsolved edge case if done using makeshift methods like the one I see below - do it cleanly on the server end or use proper HTML5 if you are focussing on reliability.
    – Rohan
    Commented Jan 6, 2014 at 12:56
  • Of course you can validate on client side without File API, Check my answer.
    – Hugeen
    Commented Jan 6, 2014 at 13:01
  • I plan on checking it server side too but I just wanted to try to prevent the upload script being executed if possible Commented Jan 6, 2014 at 13:02

3 Answers 3

7

This should check the file extension

var extension = avatar.split('.').pop().toUpperCase();
if (extension!="PNG" && extension!="JPG" && extension!="GIF" && extension!="JPEG"){
    avatarok = 0;
}

So the full code should looks like

$('#addButton').click(function () {
    var avatar = $("#avatarupload").val();
    var extension = avatar.split('.').pop().toUpperCase();
    if(avatar.length < 1) {
        avatarok = 0;
    }
    else if (extension!="PNG" && extension!="JPG" && extension!="GIF" && extension!="JPEG"){
        avatarok = 0;
        alert("invalid extension "+extension);
    }
    else {
        avatarok = 1;
    }
    if(avatarok == 1) {
        $('.formValidation').addClass("sending");
        $("#form").submit();
    }
    else {
        $('.formValidation').addClass("validationError");
    }
    return false;
});
7
  • This is a good solution if the OP is prepared to assume that users will always save their files with extensions...
    – kmoe
    Commented Jan 6, 2014 at 12:55
  • Yeah, you'd still need to check the mime type and data server side
    – Original10
    Commented Jan 6, 2014 at 12:57
  • Thanks @Original10 - This will work to display an error but it shows an error for valid files too - Any ideas why? Commented Jan 6, 2014 at 13:00
  • Just updated the answer with where the code should fit within your existing code
    – Original10
    Commented Jan 6, 2014 at 13:06
  • @Original10 - Yes that's exactly what I have but it runs the error even if I'm adding a JPEG Commented Jan 6, 2014 at 13:06
2

You can try for jquery validate for validation which is having accept :

vCategoryImage:{
   accept: "image/*"
}
0
0

As per your requirement the purpose is upload avatar ,so you can check with drop zone open source file upload extension Drop Zone.

Or else you can check with the various plugins available from Jquery.

For Example plugin please check the below links

Hope this will be helpful.

Not the answer you're looking for? Browse other questions tagged or ask your own question.