I have a form with 4 inputs (there may be many more) where the user can put a number or nothing. The only rule is that if you put a number in the input, you cannot send if the same number is on another input (without duplicates). You can send as many empty input as you want.
To check the input, I compare the length of the array of all inputs with the same array with only unique values. If they are the same length, everything is in order. I need to improve the code, because now it only works if the user enters all the input fields. If some inputs are empty, they are considered in an array with a unique value, since they all have a value of "" as a value. So, if the user enters only one number, I get that the length of this array is 4, and the array is unique - 2, but it should be 1 and 1 (skipping empty elements).
I was thinking about using splice() in arr , but is this the best way to do this check? ** EDIT: I applied splicing, but if the array is ('1', '', ''), my code gives me ('1', '') instead of just (1), as I would expect ... * * This is because splicing removes the element and changes the length of the array, so the for loop indicates the wrong index. Any ideas? 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(); } }); });
source share