2016-04-19 2 views
-2

У меня есть согласный с текстовыми полями. Таким образом, вы можете добавлять людей. И для каждого человека у вас есть textfield, где вы можете указать адрес электронной почты. Но дублирующий адрес электронной почты для другого человека: не разрешено.Проверить дублирующее значение

Например:

человек А имеет адрес электронной почты: [email protected]

человек B имеет адрес электронной почты: [email protected] ЗАПРЕЩЕНО !!

У меня есть это как JavaScript:

$('#accordion .user-row').each(function (uindex, uvalue) { 
          html += '<tr>'; 

          $(this).find('input').each(function (index, value) { 

           // Check if input type is a checkbox 
           if ($(this).is(":checkbox")) { 
            var JaNee = 'Nee'; 
            if ($(this).is(":checked")) JaNee = 'Ja'; 
            html = html + '<td>' + JaNee + '</td>'; 
           } 
           else { 
            // Add the value into the html 
            html = html + '<td>' + $(this).val() + '</td>'; 
           } 
          }); 

          html += '</tr>'; 
         }); 

это HTML:

<div class="contact-label span2"> 
      <label for="contactpersonen-email">Email adres</label> 
      <div class="contact-input-field"> 
      <input type="text" class="input-text span2" id="contactpersonen-email" name="contactpersonen-email"></input> 
      </div> 
     </div> 

Это для генерации HTML поля:

$('#add-contact p a').click(function() 
           { 
            // Make a copy of the first input fields 
            html = $('#new-contact').children().clone(); 

         // Get number of tabs in the accordion 
         var index = $('#accordion h3').length; 

            // Remove the values 
            html.find("input[type=text]").val(""); 
        html.find('input[type=checkbox]').attr('checked', false); 

         // New 'id', 'for' and 'name' attribute names 
         html.find('input[type=checkbox]').each(function() { 
          me = $(this); 
          attr = me.attr('id'); 
          number = attr.split('_')[2]; 
          newNumber = parseInt(index) + 1; 
          newAttr = attr.replace(number, newNumber); 
          me.attr('id', newAttr).attr('name', newAttr).next().attr('for', newAttr); 
         });     

            // Insert it at the end 
         $('#accordion').append(html); 
         $('#accordion').accordion('refresh'); 

         // Set last tab to active 
         $("#accordion").accordion({ active: index }); 

            // Cancel the click 
            return false; 
           }); 

У меня есть сейчас, как это :

$('#add-contact p a').click(function() 
           { 
            html = $('#new-contact').children().clone();     
            // Make a copy of the first input fields 
            var cnt = 0; 
html.find('input[type=text]').each(function() { 
     me = $(this); 
     me.attr('id', 'contactpersonen-email-'+cnt); 
     cnt++; 


}); 

так что я получаю:

contactpersonen-почта contactpersonen-почта-2, так что приятно :)

, но если создать третье текстовое поле электронной почты. это:

contactpersonen-email-2 поэтому не единственный.

у вас есть:

contactpersonen-voornaam contactpersonen-email..etc.

так каждый новый человек, он должен быть: contactpersonen-voornaam1, contactpersonen-voornaam2..etc

и:

contactpersonen-EMAIL1, contactpersonen-email2..etc.

Так что я стараюсь это так:

      // Add extra contact clicked? 
           $('#add-contact p a').click(function() 
           { 
var cnt = 0; 
var cnt2 = 0; 
var cnt3 = 0; 
var cnt4 = 0; 
var cnt5 = 0; 
var cnt6 = 0; 
      html = $('#new-contact').children().clone();     
            // Make a copy of the first input fields 

html.find('input[type=text]').each(function() { 
     me = $(this); 
     me.attr('id', 'contactpersonen-email-'+cnt); 
     cnt++; 


}); 

html.find('input[type=text]').each(function() { 
     me = $(this); 
     me.attr('id', 'contactpersonen-voornaam-'+cnt2); 
     cnt2++; 


}); 

html.find('input[type=text]').each(function() { 
     me = $(this); 
     me.attr('id', 'contactpersonen-achternaam-'+cnt3); 
     cnt3++; 


}); 

html.find('input[type=text]').each(function() { 
     me = $(this); 
     me.attr('id', 'contactpersonen-telefoon-'+cnt4); 
     cnt4++; 


}); 

html.find('input[type=text]').each(function() { 
     me = $(this); 
     me.attr('id', 'contactpersonen-functie-'+cnt5); 
     cnt5++; 


}); 

html.find('input[type=text]').each(function() { 
     me = $(this); 
     me.attr('id', 'contactpersonen-monteur-'+cnt6); 
     cnt6++; 


}); 






         // Get number of tabs in the accordion 
         var index = $('#accordion h3').length; 

            // Remove the values 
            html.find("input[type=text]").val(""); 
        html.find('input[type=checkbox]').attr('checked', false); 

         // New 'id', 'for' and 'name' attribute names 
         html.find('input[type=checkbox]').each(function() { 
          me = $(this); 
          attr = me.attr('id'); 
          number = attr.split('_')[2]; 
          newNumber = parseInt(index) + 1; 
          newAttr = attr.replace(number, newNumber); 
          me.attr('id', newAttr).attr('name', newAttr).next().attr('for', newAttr); 
         });     

            // Insert it at the end 
         $('#accordion').append(html); 
         $('#accordion').accordion('refresh'); 

         // Set last tab to active 
         $("#accordion").accordion({ active: index }); 

            // Cancel the click 
            return false; 
           }); 

Оке, я изменил к этому:

html.find('input[type=text]').each(function() { 
     me = $('#contactpersonen-email'); 
     me.attr('id', 'contactpersonen-email-'+cnt); 
     cnt++;  
}); 

но сейчас проблема в том, что на третьей добавленной человека, он по-прежнему дает: contactpersonen -email-1

и нет, но проблема теперь в том, что третьим добавленным человеком он по-прежнему дает: contactpersonen-email-2..etc

+0

Пожалуйста, пост HTML тоже. Частично работоспособная ссылка на скрипку будет отличной. – vijayP

ответ

1

Вы можете сделать так:

$(function(){ 

$('input[name^="text"]').change(function() { 

    var $current = $(this); 

    $('input[name^="text"]').each(function() { 
     if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id')) 
     { 
      alert('duplicate email address. please enter another email address.'); 
      $current.val(""); 
     } 

    }); 
    }); 
}); 


// for generating unique id example 
var cnt = 0; 
html.find('input[type=text]').each(function() { 
     me = $(this); 
     me.attr('id', 'contactpersonen-email-'+cnt); 
     cnt++; 
}); 
+0

Почему donw проголосовало за кого-то мое сообщение? Что я тогда тогда сделал? – InfinityGoesAround

+0

Так что я понимаю, что такое?Почему кто-то может проголосовать за пост без комментариев ??? Поэтому каждый может сделать это без всякой причины. Разве это не странно ??? Извините, но это действительно несправедливо. – InfinityGoesAround

+0

Но идентификаторы одинаковы. И у меня может быть два или три ... текстовых поля – InfinityGoesAround

Смежные вопросы