【问题标题】:JQuery Validate bypassed after File Upload文件上传后绕过 JQuery 验证
【发布时间】:2013-11-17 04:43:00
【问题描述】:

我正在使用 JQuery Validate 函数来验证一个表单,该表单包含几个输入字段和两个文件上传。

表单工作正常。我可以同时提交两个文件。

然后是验证问题,事情就是这样;

如果我尝试提交一个空表单,则会针对每个必填字段显示错误消息(这没问题,这就是我最初打算做的)。 但验证规则被绕过(或提交表单)当文件上传被填写时,其余必填字段为空。

这是我的 JQuery 代码:

$(document).ready(function(){

    $("#MyForm").validate({

        errorElement: 'div',
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            },
            photo: {
                required: true
            },
            cv: {
                required: true,
                extension: "pdf|doc|docx"
            }
        },
        messages: {
            name: "Please fill in your name",
            photo: "Select your image file",
            cv: "Please upload your CV in PDF format",
            email: {
                required: "Please fill in your email address",
                email: "A valid email address should look like this: name@domain.com"
            },

        },
        submitHandler: function(form) {
            form.submit();
        }
    });

});

这是我的 HTML 代码:

<form action="submit.php" method="POST" class="form-set" id="MyForm" enctype="multipart/form-data" novalidate="novalidate" >
    <fieldset>
        <legend>Your Profile</legend>
        <label class="control-label" for="cname">Name</label>
        <div class="controls">
            <input type="text" id="cname" name="name"/>
        </div>
        <label class="control-label" for="cemail">Email</label>
        <div class="controls">
            <input type="text" id="cemail" name="email"/>
        </div>
        <label class="control-label" for="cphoto">Photo</label>
        <div class="controls">
            <input type="file" id="cphoto" name="photo">
        </div>
        <label class="control-label" for="ccv">CV</label>
        <div class="controls">
            <input type="file" name="cv" id="ccv">
        </div>
    </fieldset>
</form>

谁能指出我在哪里看?

【问题讨论】:

  • 表单的 HTML 标记在哪里?
  • @Ryley 确实为我们提供了一个工作示例。 HTML表单与他提供的类似。
  • 答案中的内容并不重要。您可以为简洁的示例提供足够的代码并使您的问题完全独立。
  • 哦,对不起,我把它记在那儿让大家明白了。

标签: jquery forms jquery-validate


【解决方案1】:

两件事:

最可能的问题是您没有从 jQuery Validate 包中包含文件 additional-methods.js。这就是 extension 方法存在的地方。

其次,这只是偏好,但如果您在submitHandler 中所做的只是提交表单,那么您根本不需要拥有它 - 只需去掉该选项,它就会自动提交。

我为您的代码创建了一个工作示例,即使您正确填写了两个文件对话框,它也正确地要求输入您的姓名。 http://jsfiddle.net/ryleyb/C5k7z/

【讨论】:

  • 谢谢@Ryley :D 你的回答为我指明了正确的方向,是的,这完全是关于文件依赖项。除了所需的“additional-methods.js”之外,还包含另一个验证脚本(通过我在我的站点中使用的另一个插件)。非常感谢,我很感激。
猜你喜欢
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多