简体   繁体   English

如何将此验证 function 应用于所有输入?

[英]How can I apply this validation function to all inputs?

I have this code that works to validate just one input file, but I need to validate multiple fields (since the users can add as much "input files" as they want) and don't now how.我有这段代码可以只验证一个输入文件,但我需要验证多个字段(因为用户可以根据需要添加尽可能多的“输入文件”),现在不知道如何。 I've being trying to use loops but with no results (mostly because I don't understand them even when I've been reading it over and over in w3school).我一直在尝试使用循环,但没有结果(主要是因为即使我在 w3school 一遍又一遍地阅读它,我也不理解它们)。

https://jsfiddle.net/ElenaMcDowell/2xrqp6zg/2/ https://jsfiddle.net/ElenaMcDowell/2xrqp6zg/2/

$( ".postreplyForm" ).submit(function( event ) {
var fileBox = $('.post-file').val(); //This is the Class of the input file
if (fileBox === '') {
   //If there is no file
   return;
} else {
   //File format
   var fileExtension = ['jpg', 'jpeg', 'png', 'mp3', 'mp4', 'pdf', 'pps', 'ppt', 'pptx', 'xls', 'doc', 'docx', 'txt'];
   if ($.inArray($('.post-file').val().split('.').pop().toLowerCase(), fileExtension) == -1) {
      $(".errorFile").html('<br><center><p class="signuperror">File format not allowed.</p></center><br>');
   } else {
      $(".errorFile").html(' ');
      //File size
      var fileSize = $('.post-file')[0].files[0].size;
      if (fileSize > 1000000) {
         $(".errorFile").html('<br><center><p class="signuperror">The file is too big.</p></center><br>');
      } else {
         $(".errorFile").html(' ');
         return;
      }

   }

}
});

You can get all values using $('.post-file')[0].files then loop through each files and check condition then print some message in your div errorFiles .您可以使用$('.post-file')[0].files获取所有值,然后遍历每个文件并检查条件,然后在 div errorFiles中打印一些消息。 Also, use some variable and set it to true and if any condition doesn't meet set to false depending on this variable prevent your form to submit.此外,使用一些变量并将其设置为true ,如果任何条件不符合设置为false ,具体取决于此变量,请阻止您的表单提交。

Demo Code :演示代码

 $(".postreplyForm").submit(function(event) { var fileBox = $('.post-file').val(); if (fileBox === '') { return false; //prevent form to submit } else { //File format var fileExtension = ['jpg', 'jpeg', 'png', 'mp3', 'mp4', 'pdf', 'pps', 'ppt', 'pptx', 'xls', 'doc', 'docx', 'txt']; var flag = true; //decalre this var fileArray = $('.post-file')[0].files; //get all files $(".errorFile").html(''); //loop through files $.each(fileArray, function(i, v) { name = v.name; //name of file fileSize = v.size; //size of file console.log(name); if ($.inArray(name.split('.').pop().toLowerCase(), fileExtension) == -1) { flag = false; $(".errorFile").html('<br><center><p class="signuperror">File format not allowed.</p></center><br>'); } else { if (fileSize > 1000000) { $(".errorFile").html('<br><center><p class="signuperror">The file is too big.</p></center><br>'); flag = false; } } }) } //check not true prevnt submit if (;flag) { return false; } else { return true; } });
 .errorFile { color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form class="create-mode postreplyForm" method="post" action="a.php" name="post" enctype="multipart/form-data"> <table class="table-newpost"> <tr> <td colspan="5"><br> <div style="font-weight: bold; font-size: 14px; margin-bottom: -10px;">Attach file</div> <br> <span>File formats allowed: (.jpg, .jpeg, .png, .mp3, .mp4, .pdf, .zip, .rar, .pps, .ppt, .pptx, .xls, .doc, .docx, .txt, .rtf)</span> <br><br> <div class="fileBox"> <div style="display: flex; margin-bottom: 2px;"> <input type="file" name="file[]" id="fileID1" class="post-file" multiple="multiple"> <div id="1" class="fileBtn fileDeleteInPost" title="Delete file">x</div> <div class="fileBtn add-file" title="Add file">+</div> </div> </div> <div class="errorFile"></div> </td> </tr> <tr> <td colspan="5" style="text-align: right;"> <input type="hidden" name="reply-by" value="<?php echo $_SESSION['userId']; ?>"> <button type="submit" name="submit-postreply" id="Miau">Submit</button> </td> </tr> </table> </form>

暂无
暂无

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

相关问题 如何重复 JavaScript 表单验证,直到所有输入正确,然后将输入插入数据库? - How can I repeat JavaScript form validation until all inputs are correct, then insert the inputs in the database? 如果至少一个输入不是原始输入,如何将CSS应用于链接,如果所有输入都是原始输入,如何撤消更改? - How can I apply CSS to a link if at least one input is not original, and undo that change if all inputs are original? 如何将onFocus和onBlur事件应用于所有输入,仅在表单内选择框和文本区域 - How can I apply an onFocus and onBlur event to all inputs, select boxes and textareas within a form only 在AngularJS中,如何在`$ http`的所有响应上应用检查功能? - In AngularJS, how can I apply a check function on all the responses of `$http`? 如何将 jQuery 函数应用于具有相同 ID 的所有元素? - How can I apply a jQuery function to all elements with the same ID? 如何在表单验证中构建具有三个输入的组? - How can i build a group with three inputs in form validation? 如何恢复Tab键的正常功能并遍历所有表单输入? - How can I restore proper function of the tab key and traverse all form inputs? 将验证功能应用于所有文本字段和单选按钮 - Apply Validation Function To All TextFields and Radio Buttons 如何使此 Javascript 验证适用于多个数字字段? - How can I make this Javascript validation apply to multiple numeric fields? 如何将 SmartyStreets 验证应用于同一页面中的两个表单? - How can I apply SmartyStreets validation to two forms in the same page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM