4

the following code: script code

<script type="text/javascript">
$(document).ready(function() {
    $("#formElem").validate({

        rules: {  

            dimage:{
                minlength:200    
            }  
        },
messages: {


             dimage:{
               required: 'Please select the image!'  
            } ,

        }       
    });

});
</script>

html code

<form id="formElem" name="formElem" action="" method="post">

 <input type="file" name="dimage" id="dimage" />
</form>

I'm using jquery validate.js file for client side validation. when i create a file with above code it's not showing any error message.

How should i alert the error message when the user not selected the file?

2 Answers 2

5

If your uploading an image file Jquery Validation plugin have a option accept you can set that to a value which will validate that if the file is really an image then your jquery code will be like this:

$("#formElem").validate({

      rules: {  

            dimage:{
                    required: true,
                    accept:"jpg,png,jpeg,gif"
                }  
        },
    messages: {

            dimage:{
                    required: "Select Image",
                    accept: "Only image type jpg/png/jpeg/gif is allowed"
                }  

             }      
        });

for more information See Official Docs

1
  • Note: This method used to look at just the filename, specifically the file extension. That behaviour is now available as the "extension" method inside src/additional/extension.js. See. Commented Sep 4, 2017 at 7:34
4

Try this.

$(document).ready(function() {
    $("#formElem").validate({
        rules: {  
            dimage:{
                required: true    
            }  
        },
        messages: {
            dimage:{
               required: 'Please select the image!'  
            } ,
        }       
    });
});
1
  • 1
    how to validate image with type(png and jpg) with min and max size Commented Jul 26, 2017 at 12:59

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