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. Commented Jan 6, 2014 at 12:56
  • Of course you can validate on client side without File API, Check my answer. 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;
});
Sign up to request clarification or add additional context in comments.

7 Comments

This is a good solution if the OP is prepared to assume that users will always save their files with extensions...
Yeah, you'd still need to check the mime type and data server side
Thanks @Original10 - This will work to display an error but it shows an error for valid files too - Any ideas why?
Just updated the answer with where the code should fit within your existing code
@Original10 - Yes that's exactly what I have but it runs the error even if I'm adding a JPEG
|
2

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

vCategoryImage:{
   accept: "image/*"
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.