0

How do I validate if the user has selected an image or not on click of the upload button using jQuery. can i put restriction on the resolution of the image being uploaded?? eg:- only images above resolution 900 x 400 are allowed. Below is my form which i'm using

<form id='eventImage' action='upload_temp_image.php' method='POST' enctype='multipart/form-data'>
<input type='text' class='form-control' name='image_name' id='image_name'/>
<input type='file' name='event_image' id='event_image' />
<input type='submit' name='upload' value='Upload' />
</form>

Below is the jQuery:

$("#eventImage").validate({
        rules: {
            image_name: {
                required : true
            },
            // validate the input type=file 
        },

        image_name: {
            new_name: "Please enter a Category name!",
            event_image: "Please select an image"
        }
  });
1

2 Answers 2

1

Have a look at the extension rule

$("#eventImage").validate({
    rules: {
        image_name: {
            required: true
        },
        event_image: {
            required: true,
            extension: "png|jpg"
        }
    },
    messages: {
        event_image: {
            extension: "Please select an image"
        }
    }
});

Demo: Fiddle

Or if you want to go with mimetype, then look at accept rule

$("#eventImage").validate({
    rules: {
        image_name: {
            required: true
        },
        event_image: {
            required: true,
            accept: "image/*"
        }
    },
    messages: {
        event_image: {
            accept: "Please select an image"
        }
    }
});

Demo: Fiddle

0
0

hey I don't know if people still have this problem but if you want the user to upload only images and nothing else just add accept="image/*" after <input type="file" and it works.

No need to add jquery for that.

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