twitterfacebookgoogle plusrss feed

Pages

Wednesday, July 4, 2012

Restrict file extension using JQuery


$(function() {
    $("input[type=button]").click(function() {
        if (!$("input[type=file]").val()) {
            alert("Select file to Upload !");
            return false;
        }
        var allowedTypes = ["jpg", "jpeg", "gif", "png"];

        var path = $("input[type=file]").val();
        var ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();

        if (allowedTypes.lastIndexOf(ext) == -1) {
            alert("Select valid file i.e.\n" + allowedTypes);
        }
        alert("Valid file selected");
    });
});

0 comments:

Post a Comment

comment or ask