javascript - jquery - check to see if files are the same -


i inherited webapp allows users submit 7 files @ time. there code works in checking see if there 2 of same file, bulky , not flexible.

if (elementid == "file2") {          if ((form.file2.value == form.file1.value) ||         (form.file2.value == form.file3.value) ||         (form.file2.value == form.file5.value) ||         (form.file2.value == form.file6.value) ||         (form.file2.value == form.file7.value)) {         alert("error!  file - " + form.file2.value + "\nselected more once.  select file.");         validfile = "false";          } } 

if want add file or remove file, have alter javascript/jquery work new code. want make flexible.

in other places in code have been able cut on code check file name against date submitted see if match

$("[id^=file]").filter(function() {     if ($(this).val().length > 0){         if($(this).val().search(reportingperiod) < 0 ) {            alert("error! " + $(this).get(0).id + " not match selected reporting period!");            $(this).focus();            validform = false;         }                                                    } }); 

i thinking use along these lines checking file names, when $(this).val(), return first file name , not of them. can use same idea , put file names array , dup check there? if how? or there better way want to?

there bunch of ways it. 1 way have common class , loop on values , matches.

$(".test").on("change", function() {      var allfields = $(".test").removeclass("error");  //get text fields      var vals = allfields.map( function() { return this.value; }).get();  //get values array      while(vals.length>1) {  //loop until have 1 value left         var val = vals.pop();  //pull off index see if exists         if (vals.indexof(val)>-1) {  //if exists, find others , set error             allfields.not(".error").filter( function(){ return this.value==val; }).addclass("error");         }      }        });
.error { background-color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="text" value="1" class="test" />  <input type="text" value="2" class="test" />  <input type="text" value="3" class="test" />  <input type="text" value="4" class="test" />  <input type="text" value="5" class="test" />


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -