jQuery验证 - 即使指示了错误的文件类型,表单也会提交

时间:2015-01-04 11:46:18

标签: jquery jquery-validate

我为我的文件字段编写了以下规则,因为我希望只接受PDF文件的Word文档。

paperupload:{required: true, accept: "application/msword, application/pdf"}

当我使用上传的JPG文件进行测试时,表单也会在不验证其他字段的情况下提交。我在这里做错了吗?

$("#papersubmitform").validate(
    {
        submitHandler: function(form) {

                        $(form).submit();

        },

        messages: {
            //CHECK Correspondence Authors' Information
            contactTitle: "Please specify Correspondence Author's Title",
            contactFirstName: "Please specify Correspondence Author's First Name",
            contactLastName: "Please specify Correspondence Author's Last Name",
            contactEmail: "Please specify Correspondence Author's Email Address",
            contactUniList: "Please specify Correspondence Author's University",
            //paperTitle:"Please specify Paper Title",
            paperAbstract:"Please specify Paper Abstract"

        },

        rules:{
            //Rules for Correspondence Author's Information
            contactTitle: {required: true},
            contactFirstName: { required:true},
            contactLastName: { required:true},  
            contactEmail: {required:true, email: true},
            contactUniList:{ required:function(){
                if(checkuninotinlist){ return false; }
                    else { return true; }
                }
            },

            contactUniversityName:{ required: function(){
                    if(checkuninotinlist){ return true; }
                    else { return false; }
                    }
            },

            contactUniCountry:{ required: function(){
                    if(checkuninotinlist){ return true; }
                    else { return false; }
                    }
            },


            contactOrganisation: { required:function(){ 
                    if(checkorganisation){ return true; }
                    else { return false; }
                    }
            },

            contactOrgCountry: { required:function(){ 
                    if(checkorganisation){ return true; }
                    else { return false; }
                    }
            },

            mailinglist:{required:true},
            researchmethod:{required:true},

            //PAPER INFORMATION
            paperTitle:{required:true},
            paperAbstract:{required:true},
            paperupload:{required: true, accept: "application/msword, application/pdf"}

    }
 });            

HTML HERE

<input type="file" name="paperupload" />

1 个答案:

答案 0 :(得分:1)

您必须包含the additional-methods.js file才能使用accept方法。

在包含jQuery Validate插件后立即包含它。

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" src="additional-methods.js"></script>