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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
4

Try this.

$(document).ready(function() {
    $("#formElem").validate({
        rules: {  
            dimage:{
                required: true    
            }  
        },
        messages: {
            dimage:{
               required: 'Please select the image!'  
            } ,
        }       
    });
});

1 Comment

how to validate image with type(png and jpg) with min and max size

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.