2015-02-09 101 views
1

我有一个4输入(甚至可以更多)的表单,用户可以放置一个数字或什么都不是。唯一的规则是,如果您在输入中输入数字,则如果在另一个输入中输入相同的数字(无重复),则无法提交数字。您可以根据需要提交尽可能多的空输入。基于长度比较两个数组:跳过空值

为了验证输入,我只用唯一值比较了具有相同数组的所有输入数组的长度。如果他们有相同的长度没关系。 我需要改进我的代码,因为现在只有当用户输入所有输入字段时才有效。如果某些输入为空,则它们在数组中被视为具有唯一值,因为它们都具有“”作为值。所以,如果用户只输入一个数字,我会得到数组长度为4,数组唯一为2,但它应该是1和1(跳过空白项)。

我在考虑在arr上使用splice(),但是这是做这种验证的最好方法吗? **编辑:我应用拼接,但如果数组是('1','','')我的代码给我('1',''),而不仅仅是(1),因为我期望... **这是因为拼接删除项目并更改数组长度,以便for循环指向错误的索引。 有什么想法? HTML:

<div class="sez-form"> 
    <fieldset> 
     <legend>Messaggi inclusi</legend> 
     <div class="percheckbox"> 
      <input class="checkseq" type="checkbox" value="1" name="messaggio[0]"> 
      Prova di messaggio che scorre<br> 
      <label>Ordine: </label> 
      <input class="seq" type="text" name="ordine[0]" maxlength="2" size="2"> 
     </div> 

     <div class="percheckbox"> 
      <input class="checkseq" type="checkbox" value="3" name="messaggio[1]"> 
      Titoli di film<br> 
      <label>Ordine: </label> 
      <input class="seq" type="text" name="ordine[1]" maxlength="2" size="2"> 
     </div> 

     <div class="percheckbox"> 
      <input class="checkseq" type="checkbox" value="6" name="messaggio[2]"> 
      Prova a testo fisso<br> 
      <label>Ordine: </label> 
      <input class="seq" type="text" name="ordine[2]" maxlength="2" size="2"> 
     </div> 
     <br style="clear: both;"> 
    </fieldset> 
</div> 

JAVASCRIPT:

function uniqueArray(arr) { 
    return $.grep(arr,function(v,k) { 
     return $.inArray(v,arr) === k; 
    }); 
} 

$(document).ready(function() { 
    $('#invia').click(function(e) { 
     e.preventDefault(); 
     var arr = $(".seq").map(function(){ return $(this).val(); }).toArray(); 
     var empty = $(".seq").filter(function() { 
      return this.value == ""; 
     }) 
    for (index = 0; index < arr.length; ++index) { 
     if (arr[index]=='') { 
      new_arr = arr.splice([index],1); 
     } 
     console.log(arr); 
    } 

     if(empty.length == $('.seq').length) { 
      alert('Non hai scelto alcun messaggio per il workflow. Correggi per procedere.'); 
     } 
     else if(uniqueArray(arr).length != $('.seq').length) { 
      console.log(uniqueArray(arr)); 
      alert('Ci sono voci duplicate nella sequenza. Correggi per procedere.'); 
     } 
     else if($('#dt_from').val()=='__/__/____ __:__') { 
      alert('Scegli data e ora di inizio validit\u00E0 per il workflow'); 
     } 
     else if($('#dt_to').val()=='__/__/____ __:__') { 
      alert('Scegli data e ora di fine validit\u00E0 per il workflow'); 
     } 
     else { 
      ajaxSubmit(); 
     } 
    }); 
}); 

回答

0

使用散列为你迭代跟踪值。此示例仅返回truefalse,但您也可以扫描整个数组并返回重复的值。

function uniquifyArray(ary) { 
    var seen = {}; 
    var isUnique = true; 
    /* iterate backwards since the array length will change as elements are removed */ 
    for (var i=ary.length; i--;) { 
     /* remove blank/undefined */ 
     if (typeof ary[i] === 'undefined' || ary[i] === '') {     
      ary.splice(i,1); 
     } else { 
      /* check if this value has already been seen */ 
      if (ary[i] in seen) { 
       isUnique = false; 
       ary.splice(i,1); 
      } else { 
       seen[ary[i]]=true; 
      } 
     } 
    } 
    ary = ary.sort(); 
    return isUnique; 
} 

var test = [ '1','2','','','3','4','1' ]; 
uniquifyArray(test); // returns false, test = [ '1','2','3','4' ] 

test = [ '1','2','','' ] 
uniquifyArray(test); //true, test = ['1','2'] 
+0

If thisUniqueArray(['1','2','','','3','4','1']);是用户输入它应该给我isUniqueArray(['1','2','','3','4']);这是错误的',因为输入是7项,输出只有5(存在重复)。我想排序输入,在isUniqueArray(['1','2','3','4','1'))中更改它。即5项 – 2015-02-09 15:49:38

+0

这是一个验证函数 - 根本不修改这些值。看来你想要一个功能,将排序,剥离空白_and_失败,如果有重复? – 2015-02-09 15:56:03

+0

是的,现在我检查arr和arrUnique的长度......空白项目应该被跳过,因为我可以发布一个数组,甚至有一个值,两个空,但我不能将这个选项添加到我的代码 – 2015-02-09 15:58:24

1

也许我不明白你正在尝试做的,但你为什么不能喜欢的东西做得很干脆:

$('#invia').click(function(e) { 
    e.preventDefault(); 
    var unique = [], nonunique = []; 
    $(".seq").each(function(index){ 
     var val = $(this).val(); 
     if (val !== "") { 
      if ($.inArray(val, unique) !== -1) { 
       nonunique.push(val); 
      } else { 
       unique.push(val); 
      } 
     } 
    }); 
    // If unique and nonunique are empty, all inputs were blank 
    // else if nonunique is empty, inputs are valid and in unique 
}); 
+0

将尝试。或多或少,它符合我的要求。或者甚至在我的两个验证中。会尝试让你知道! – 2015-02-09 16:25:03

+0

也不错。我选择了另一个答案只是因为它接近我的实际代码。非常感谢双方! – 2015-02-09 16:27:23

+0

我注意到我输入得太快,忘记提取值。我修正了这个例子。 – bobdye 2015-02-09 16:27:42

1

这里是来处理它另一种可能的方式。 Here is the working JSFiddle.这里是代码:

$(function() { 
    $("#submit").click(function() { 
     //build a profile of the inputs 
     var inputs = []; 
     var values = []; 
     var dups = false; //track duplicates on pass 1 

     $(".seq").each(function(i, el) { 
      var empty = (el.value == ""); //check if empty 
      var exists = (!empty && $.grep(inputs, function(item, index) { 
       return (item.Value === el.value); 
      }).length > 0); //check if exists 
      dups = (dups || exists); //track dups 

      //add the new input item 
      var obj = { 
       Element: el, 
       Value: el.value, 
       Empty: empty, 
       Exists: exists 
      }; 
      inputs.push(obj); 

      //conditionally add the sorting value 
      if (!empty && !exists) 
       values.push(el.value); 
     }); 

     //Validate the inputs. If there are duplicates, don't submit 
     $(".seq").css("background-color", "white"); //clear errors 
     if (dups) { 
      $(inputs).each(function(i, el) { 
       if (el.Exists) 
        el.Element.style.backgroundColor = "red"; 
      }); 
     } else { 
      values = values.sort(); 
      alert(values); 
     } 
    }); 
}); 

使用这种方法,在结束时,你有一个数组 - inputs - 所有与他们的状态元素的,这样就可以提供错误处理的具体领域。在我的例子中,错误字段变为红色。

alert处,您有一个有序值的排序数组。