简体   繁体   English

输入文件类型错误验证无法使用jQuery

[英]input file type error validation not working using jquery

Currently working on input file error validation When i searched about the validation i have found jquery validation so i have started using it and again when i searched about how to validate the input file i have got some useful information from SO Based on that I have created error validation page for input file. 当前正在处理输入文件错误验证当我搜索有关验证的信息时,我发现了jquery验证,因此我开始使用它,而当我搜索有关如何验证输入文件的信息时,我又从SO获得了一些有用的信息,这是基于我创建的输入文件的错误验证页面。 With my current code I can able to upload pdf & Jpeg file and view the file but the validation was not happening if user click next button without uploading any file it should say you have 2 files missed if the user upload one file and he click next button it should say you have 1 file miss. 使用我当前的代码,我可以上传pdf&Jpeg文件并查看文件,但是如果用户单击“下一步”按钮而不上传任何文件,则不会进行验证,如果用户上传一个文件然后单击“下一步”,则应该说您丢失了2个文件按钮,它应该说您有1个文件未命中。 I have tried giving required in the html input type field and tried giving required in jquery validation nothing was working. 我已经尝试在html输入类型字段中提供了required,并尝试在jquery验证中提供了要求,但没有任何效果。

Here is my jquery code 这是我的jQuery代码

   $(".attachForm").validate({
        ignore: false,
         onkeyup: false,
            showErrors: function (errorMap, errorList) {
            var errors = this.numberOfInvalids();

            if (errors) {
                var message = errors === 0 ? 'You missed 1 field. It has been highlighted' : 'You have missed ' + errors + ' fields. Please fill before submitted.';
                $("#error_message").html(message);
                $(".error_msge").show();
            } else {
                $(".error_msge").hide();
            }
            this.defaultShowErrors();
        },
        errorPlacement: function () {
            return false;
        },
        highlight: function (element) {

            if($('input').attr('type') == 'checkbox') {

            } else {
               $(element).addClass('errRed');
                $(".file_ipt").addClass('errRed');
            }
            $(element).prevAll('label').find('span.required-star').addClass('text-error-red').removeClass('text-error-black');            
        },
        unhighlight: function (element) {

            if($('input').attr('type') == 'checkbox') {
            } else {
                $(element).removeClass('errRed');
                $(".file_ipt").addClass('errRed');
            }
            $(element).prevAll('label').find('span.required-star').addClass('text-error-black').removeClass('text-error-red');

        },rules: {
        apt_code:"required",
        apt_cer:"required",
        checkfile:"required"
        },
        submitHandler: function (form) { // for demo
        alert('valid form submitted'); // for demo
        return false; // for demo
    }
    }); 

I tried changing the name in all field but no use 我尝试在所有字段中更改名称,但没有用

Here is the fiddle link for the detailed code 这是详细代码的小提琴链接

Kindly please suggest me. 请给我建议。 kindly guide as i am not getting any stuff :( 友善的指导,因为我没有得到任何东西:(

Thanks for looking the question. 感谢您查询问题。

You have to assign the unique name attribute to each <input type="file" class="checkfile"> 你必须分配的唯一name属性到每个<input type="file" class="checkfile">

<input type="file" class="checkfile" name="file_alpha">
<input type="file" class="checkfile" name="file_beta">

and then in rules you have to define both fields and make sure they are required 然后在rules必须定义两个字段并确保它们是required

rules: {
   file_alpha: {
        checkfile: "required",
        required: true,
   },
   file_beta: {
        checkfile: "required",
        required: true,
   }
},

Fiddle 小提琴

Correct Solution 正确的解决方案

Above solution will work because assigning the unique name and required rules set will trigger the validation but will not return the desired result because OP trying to validate the input with same name attribute and triggering the error counter according to number of invalid input fields. 上面的解决方案将起作用,因为分配唯一的namerequired规则集将触发验证,但不会返回期望的结果,因为OP试图验证具有相同name属性的输入并根据无效输入字段的数量触发错误计数器。

Reason the validation not working in original code because no required rules 原因是验证没有原始规则,因为没有必需的规则

rules: {
    checkfile:"required"
},

defined anywhere. 在任何地方定义。

so work around is set required rules and add to inputs with same name attribute OR type using jQuery each() function 因此,解决方法是设置required规则,并使用jQuery each()函数将其添加到具有相同name属性或type输入中

$("input[type=file]").each(function() {
    $(this).rules("add", {
        required: true,
    });
});

and validation will work, errors will triggered with counter and on validating the input field, error counter decrease as like OP's desired output. 并且验证将起作用,错误将通过计数器触发,并且在验证输入字段时,错误计数器会减少,就像OP的期望输出一样。

Fiddle Proper Working Example 小提琴正确的工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM