Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate form input type file type using jquery validate plugin

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?

like image 634
MAK Avatar asked Jan 17 '23 12:01

MAK


1 Answers

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

like image 150
Seeker Avatar answered Jan 20 '23 16:01

Seeker