1

我在 wordpress 中使用这个脚本。

这是我的代码:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://raw.github.com/jzaefferer/jquery-validation/master/jquery.validate.js"></script>
<script src="https://raw.github.com/jzaefferer/jquery-validation/master/additional-methods.js"></script>
<script>
            jQuery(document).ready(function($){
                $("#subsciptionForm").validate({
                    rules: {
                        headshot: {
                            required: true,
                            accept: "jpg,png,jpeg,gif"
                        },
                        actorcv: {
                            required: true,
                            accept: "application/msword, application/pdf"
                        }
                    },
                    messages: {
                        headshot: {
                            required: 'Select an image to upload',
                            accept: 'Only images with type jpg/png/jpeg/gif are allowed'
                        },
                        actorcv: {
                            required: "Select a CV",
                            accept: 'Only doc/docx/pdf files are allowed'
                        }
                    },
                    submitHandler: function(form) {
                        //form.submit();
                        var url = '<?php echo SET_SUBSCRIBER; ?>';
                        var datastring = $("form").serialize();
                        alert(datastring); return false;              
                        $.ajax({
                            type: "POST",
                            url: url,
                            data: datastring,
                            success: function(data) {
                                //alert(data); return false;
                                form.submit();
                            }
                        });
                        return false;
                    }
                });
            });
        </script>

这是表单域

                        <!-- Upload Headshot -->
                    <tr>            
                        <td class="title_cell" width="23%">
                            Upload Headshot :<span class="required">*</span>
                        </td>
                        <td class="field_cell">
                            <input type="file" class="required" name="headshot" size="25"> (jpg, gif or png only, with maximum 1MB size)
                        </td>
                    </tr>
                    <!-- Upload Actor's CV -->
                    <tr>            
                        <td class="title_cell" width="23%">
                            Upload Actor's CV :<span class="required">*</span>
                        </td>
                        <td class="field_cell">
                            <input type="file" class="required" name="actorcv" size="25"> (MS-word or PDF only, with maximum 1MB size)
                        </td>
                    </tr>

它适用于图像文件验证,但不验证 pdf 和 doc 文件。继续提供我在消息“ Only doc/docx/pdf files are allowed”中定义的相同消息。我也在控制台中得到这个:

TypeError: b.browser is undefined

编辑:

在 Kevin B 的评论帮助后 TypeError 消失了,但它仍然没有验证 pdf 文件。任何想法 ?

4

1 回答 1

2

accept规则用于通过 mime-type 进行验证

如果您尝试通过文件扩展名进行验证,则需要使用该extension规则。

请参阅: http ://docs.jquery.com/Plugins/Validation/CustomMethods/extension#extension

rules: {
    headshot: {
        required: true,
        extension: "jpg,png,jpeg,gif"
    },
    actorcv: {
        required: true,
        accept: "application/msword, application/pdf"
    }
},
于 2013-04-17T04:20:39.777 回答