Javascript 检查输入电子邮件值是否与其他输入电子邮件值相同

Javascript 检查输入电子邮件值是否与其他输入电子邮件值相同,javascript,jquery,Javascript,Jquery,我尝试检查电子邮件的输入值是否与其他电子邮件的输入值相同。您可以像手风琴一样添加人员。所以每个人都有一封电子邮件。但不可能有两个或多个用户在文本框中输入相同的电子邮件地址 我有这个: $(document).ready(function() { // Form validation $(".klantregistratie form").validate({ rules: { verploegen_form_klantregistratie_Bedrijfs

我尝试检查电子邮件的输入值是否与其他电子邮件的输入值相同。您可以像手风琴一样添加人员。所以每个人都有一封电子邮件。但不可能有两个或多个用户在文本框中输入相同的电子邮件地址

我有这个:

 $(document).ready(function() {
   // Form validation
   $(".klantregistratie form").validate({
     rules: {
       verploegen_form_klantregistratie_Bedrijfsnaam: "required",
       verploegen_form_klantregistratie_Postcode: "required",
       verploegen_form_klantregistratie_Plaats: "required",
       verploegen_form_klantregistratie_Emailadres: {
         required: true,
         email: true
       },
       verploegen_form_klantregistratie_KvK_nummer: "required",
       verploegen_form_klantregistratie_naam_eigenaar: "required",
       verploegen_form_klantregistratie_Telefoon_mobiel: "required"
     },
     messages: {
       verploegen_form_klantregistratie_Bedrijfsnaam: "De bedrijfsnaam is niet ingevuld",
       verploegen_form_klantregistratie_Postcode: "De postcode is niet ingevuld",
       verploegen_form_klantregistratie_Plaats: "De plaats is niet ingevuld",
       verploegen_form_klantregistratie_Emailadres: {
         required: "Je hebt geen email adres ingevuld",
         email: "Je hebt geen geldig email adres ingevuld"
       },
       verploegen_form_klantregistratie_KvK_nummer: "Het KVK nummer is niet ingevuld",
       verploegen_form_klantregistratie_naam_eigenaar: "De naam van de eigenaar is niet ingevuld",
       verploegen_form_klantregistratie_Telefoon_mobiel: "Het (mobiele) telefoon nummer is niet ingevuld"
     }
   });

   // Hide the textarea
   $('#verploegen_form_klantregistratie_Contactpersonen').parent().parent().hide();

   $('input[name=FormCH1_h]').val(x);

   // Add extra contact clicked?
   $('#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;
   });

   // Submit clicked?
   $('.submit-button input').click(function() {
     // Set variables
     var html = '';
     var i = 1;

     // Loop through all the input fields for contacts
     $('#accordion .user-row').each(function(uindex, uvalue) {
       html += '<tr>';

       if (($('#contactpersonen-email').attr('value')) == $('#contactpersonen-email').attr('value')); {
         alert('the same');
       }

       $(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>';
     });

     // Set the value from the textarea to the generated html
     $('#verploegen_form_klantregistratie_Contactpersonen').val('<table>' + html + '</table>');
   });
 });

 $(function() {
   $("#accordion").accordion({
     heightStyle: "content"
   });
 });

这也不起作用:

  if(($('#contactpersonen-email').val()) ==  $('#contactpersonen-email').val() )
                {
                        alert('the same');
                    }
我现在是这样的:

if (($('#contactpersonen-email').attr('value')) == $('#contactpersonen-email').attr('value')); {
    alert('the same');
}
<![CDATA[
                            $(document).ready(function()
                            {
                                // Form validation
                                $(".klantregistratie form").validate({
                                    rules: {
                                        verploegen_form_klantregistratie_Bedrijfsnaam: "required",
                                        verploegen_form_klantregistratie_Postcode: "required",
                                        verploegen_form_klantregistratie_Plaats: "required",
                                        verploegen_form_klantregistratie_Emailadres: {
                                            required: true,
                                            email: true
                                        },
                                        verploegen_form_klantregistratie_KvK_nummer: "required",
                                        verploegen_form_klantregistratie_naam_eigenaar: "required",
                                        verploegen_form_klantregistratie_Telefoon_mobiel: "required"
                                    },
                                    messages: {
                                        verploegen_form_klantregistratie_Bedrijfsnaam: "De bedrijfsnaam is niet ingevuld",
                                        verploegen_form_klantregistratie_Postcode: "De postcode is niet ingevuld",
                                        verploegen_form_klantregistratie_Plaats: "De plaats is niet ingevuld",
                                        verploegen_form_klantregistratie_Emailadres: {
                                            required: "Je hebt geen email adres ingevuld",
                                            email: "Je hebt geen geldig email adres ingevuld"
                                        },
                                        verploegen_form_klantregistratie_KvK_nummer: "Het KVK nummer is niet ingevuld",
                                        verploegen_form_klantregistratie_naam_eigenaar: "De naam van de eigenaar is niet ingevuld",
                                        verploegen_form_klantregistratie_Telefoon_mobiel: "Het (mobiele) telefoon nummer is niet ingevuld"
                                    }
                                });

                                // Hide the textarea
                                $('#verploegen_form_klantregistratie_Contactpersonen').parent().parent().hide();

                                // Add extra contact clicked?
                                $('#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;
                                });

                                // Submit clicked?
                                $('.submit-button input').click(function()
                                {
                                    // Set variables
                                    var html = '';
                                    var i = 1;

                        // Loop through all the input fields for contacts
                        $('#accordion .user-row').each(function (uindex, uvalue) {
           //Niels                      
                    var mails = $('.contactpersonen-email').map(function(){
                     return $(this).val();
    //console.log($(this).val());
                    }).get(); 

Array.prototype.areUnique = function() {
    var l = this.length;
    for(var i=0; i<l; i++) {
        for(var j=i+1; j<l; j++) {
            if (this[i] === this[j]) return false;
        }
    }
    return true;
};

if (!mails.areUnique()) {
    alert('values are not unique');
} else {
    alert('values are unique');
}

klant worden:661[“email 1”]

if语句末尾的分号:

        if(($('#contactpersonen-email').attr('value')) ==  $('#contactpersonen-email').attr('value') );
                            {
                                    alert('the same');
                            }
应该是:

        if(($('#contactpersonen-email').attr('value')) ==  $('#contactpersonen-email').attr('value') )
                            {
                                    alert('the same');
                            }
在条件测试imho的同一行上添加大括号是一个很好的论据。

尝试使用
.val()
而不是
.attr('value')

.attr('value')
将在编辑前为您提供值,而
.val()
将为您提供当前值。

问题是,
id
html属性必须是唯一的。您应该为表单的“联系人”部分中的字段指定一个唯一的id,并改用类:

<input type="email" class="contactpersonen-email" id="contactpersonen-email0" />
然后可以使用此简单函数检查所有值是否唯一:

Array.prototype.areUnique = function() {
    var l = this.length;
    for(var i=0; i<l; i++) {
        for(var j=i+1; j<l; j++) {
            if (this[i] === this[j]) return false;
        }
    }
    return true;
};

哦,天哪,凹痕。你的标签键看起来有多糟?反正不是我的标签。这就是我得到这个的文件。该程序中没有任何语法或其他内容。很抱歉,有这么多工具可以自动更正缩进。因此,如果您希望得到帮助,请花时间格式化您的代码,使其可读。@brso05-这不会有任何效果,因为
$('contactpersonen email')
将始终===
$('contactpersonen email')
将是删除整个块的更好建议。好的,我删除了分号。但每次警报消息都是:P,当然,你要检查一个值是否等于它本身,它将始终等于。可以将其与不同的值进行比较:P@Hector在末尾添加大括号有什么帮助?这仍然有效,但没有给出预期的:
if(false);{\n alert(“true”);\n}
(\n表示换行符,因为这是注释)那么它是什么呢??我真的不明白为什么它不起作用。我这么做了:如果($($)('contactpersonen email').val())=$('contactpersonen email').val()){alert('same');}@savantheincracable-你应该更加注意,伙计。现在你有了额外的多年生。你还在把完全一样的东西和它自己做比较。?那你有什么建议?非常感谢。这是一个预告,谢谢你。我会尝试一下,但是如何在jquery.validate插件中实现呢?但是只显示电子邮件的第一个值。如果生成两个联系人,您将收到:[“jj”]klant worden:655[“jj”]我不确定是否理解,但问题是如何检查两个输入是否具有相同的值,不是吗?是的,但我总是得到相同的值。即使我在电子邮件文本fiedls中输入了两个不同的值:因为我首先检查是否会返回正确的电子邮件。看看我的编辑帖子
var mails = $('.contactpersonen-email').map(function(){
    return $(this).val();
}).get(); 
/* "values" now contains an array like ['mail1@website.tld', 'mail2@website.tld', 'mail2@website.tld'] */
Array.prototype.areUnique = function() {
    var l = this.length;
    for(var i=0; i<l; i++) {
        for(var j=i+1; j<l; j++) {
            if (this[i] === this[j]) return false;
        }
    }
    return true;
};
if (!mails.areUnique()) {
    alert('values are not unique');
} else {
    alert('values are unique');
}