1

i try validate csv file when user upload file with extension/format CSV

<script type="text/javascript">
$(document).ready(function() {
    $('#form1').validate({
        rules: { csv: { required: true, accept: "csv"  }},
        messages: { csv: "File must be CSV" }
    }); 
});
</script>

<form action="product_import.php?action=import" method="POST" name="form1" id="form1" enctype="multipart/form-data">
<input name="csv" type="file" id="csv" />
</form>

But the problem is when i upload file with extension csv this error message will show up File must be CSV , i dont know why this script can't accept even i set to accept: "csv" .

My csv file name is product.csv

0

2 Answers 2

10

The accept rule is not for file extensions, it's for Mime types.

You might want to use the extension rule instead

DEMO: http://jsfiddle.net/c5R3b/

$(document).ready(function () {

    $('#form1').validate({
        rules: {
            myfield: {
                required: true,
                extension: "csv"
            }
        }
    });

});

However, both of these rules require the inclusion of the additional-methods.js file.

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

1 Comment

thanks @Sparky .. i already include additional-methods.js file just need to change extension: "csv" .. again thanks :)
4

Here is something worked for me with plain JS approach. Might be helpful for someone,

if( document.getElementById("csv").value.toLowerCase().lastIndexOf(".csv")==-1) 
{
        alert("Please upload a file with .csv extension.");
        return false;
}

1 Comment

Just what I was looking for. This is great!

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.