0

I tried this, Its not working.

I want to do following things:

While clicking on submit button the profilePic should be validate. profilePic must not be null and it must accept only one of the

gif|png|jpg|jpeg|pjpeg

file.

I also want to add maximum size for uploading.

 $("#basicprofile").bind("submit", function() {
            $("#profilePic").validate({
                rules: {
                    field: {
                        required: true,
                        accept: "gif|png|jpg|jpeg|pjpeg"
                    }
                }
            });
        });


 <s:form action="basicprofile" method="post" enctype="multipart/form-data" 
  id="basicprofile">
       <s:file name="profilePic"  id="profilePic" label="Profile Picture" />
       <s:submit />
 </s:form>
Pigueiras
  • 18,778
  • 10
  • 64
  • 87
xrcwrn
  • 5,339
  • 17
  • 68
  • 129

2 Answers2

1
$(document).ready(function() {

    $("#basicprofile").validate({
        rules: {
            profilePic: {
                required: true,
                accept: "gif|png|jpg|jpeg|pjpeg"                
            }
        },

        messages: {
            profilePic: {
                required: "Empty file",
                accept: "Wrong format!!"
            }                  
        },

        errorPlacement: function(error, element) {
            $("#error-messages").html(error); // error container
        }

    });

});

You can check that the field is Filled and file have correct format in browser, but check filessize is hard problem. You have to do it with AJAX, and use the filesize headers sent by the browser to the server on the POST request.

Yahoo's UI Library has a tool to help with this. YUI Uploader

Example (without check file size): http://jsfiddle.net/burlak/U36JS/

Burlak Ilia
  • 185
  • 4
0

You have to validate the form, not the field. I believe it has to be something like:

$("#basicprofile").validate({
    rules: {
        profilePic: {
            required: true,
            accept: "gif|png|jpg|jpeg|pjpeg"
        }
    }
});
Pigueiras
  • 18,778
  • 10
  • 64
  • 87
  • @Manish Have you looked in your favorite debugger tool for javascript if there is an error (for instance: Google Chrome Developer Tools -> Console) – Pigueiras Dec 03 '12 at 16:08
  • @Manish I also changed my answer, I used validation with jquery a time ago, so I can't remember well how to bind the validate event. I think there is no need of binding the `submit` event (just like Burlak answer). – Pigueiras Dec 03 '12 at 16:11
  • I think, it's not work, because you init validate before your dom ready. Try use it in $(document).ready(...) – Burlak Ilia Dec 04 '12 at 06:42
  • @BurlakIlia If you put the script right before `

    `, there is no need of calling `$(document).ready(...)`.

    – Pigueiras Dec 04 '12 at 07:52