1

I try to check if input value of email is the same with other input value of email.You can add persons like a accordian. So every person has an email. But it is not possible that two or more put the same email address in the text box

I have this:

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

So I try to compare like this:

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

enter image description here

This also doesnt work:

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

I have it now like this:

<![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');
}

//Niels

But every time it says Vlaues are unique

I have it liket this:

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

klant-worden:661 ["email 1"]

InfinityGoesAround
  • 1,011
  • 4
  • 17
  • 31

3 Answers3

1

Try using .val() instead of .attr('value').

.attr('value') will give you the value before editing, while .val() will give you the current value.

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
0

semicolon at end of if statement:

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

should be:

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

It's a good argument for adding opening braces ont he same line as the conditional test, imho.

Hektor
  • 1,845
  • 15
  • 19
0

jsFiddle demo

Problem is, id html attribute have to be unique. You should give a unique id to your fields inside the "contact" part of your form and use a class instead :

<input type="email" class="contactpersonen-email" id="contactpersonen-email0" />

Then, you retrieve all values of the .contactpersonen-email fields :

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'] */

Then you can use this simple function to check if all values are unique :

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;
};

At last, just do this simple test :

if (!mails.areUnique()) {
    alert('values are not unique');
} else {
    alert('values are unique');
}
Brewal
  • 8,067
  • 2
  • 24
  • 37