Compare the number of occurrences of an Array in Javascript dynamic fields before submitting form data

Asked

Viewed 209 times

1

How can I compare the number of occurrences of Array in dynamic fields Javascript before sending the form data. I need to check if both are equal. I can do it only after sending with the function Count of PHP, I would like to know if there is any way to do this before sending, and if possible, via Javascript, because it is already the validation of the form. How to count these values of the Array before sending?

Dynamic fields:

<script type="text/javascript">

    // Verificar se ambos Array tem o mesmo número de ocorrências 
    $(document).ready(function() {
    $("#mail_form").submit( function() {

        var qtdmyfoto = document.querySelectorAll('name="myfoto[]"').length;
        var qtdreferencia = document.querySelectorAll('name="referencia[]"').length;

        if(qtdmyfoto != qtdreferencia)
        {
        alert("Falta alguma coisa");
        return false;
        }   

    });

});
</script>

<form id="mail_form" role="form" action="">
       .
       .
       .
       .
   <script type="text/javascript"> 
       .
       .
       .
       .
   $(wrapper).append('<div>'
        +'<div>'
            +'<br><label class="control-label">Foto: '+z +'</label>'
            +'<input type="file"  name="myfoto[]"  /> '
        +'</div>'

        +'<div>'
            +'<br><label class="control-label">Referência Foto '+z +'</label>'
            +'<input type="text" class="form-control" name="referencia[]">'
        +'</div>'    
    +'</div>');
    </script>
</form>

I’m unsuccessfully trying something like:

$(document).ready(function() {
    $("#mail_form").submit( function() {

        if(referencia.array.length != myfoto.array.length)
        {
            alert("Falta alguma coisa");
            return false;
        }

    });

});
  • 2

    We don’t know what the variable is myfoto nor referencia... can you explain? do you want to know how many elements name="myfoto[]" is there? In that case var qtd = document.querySelectorAll('name="myfoto[]"').length;

  • myfoto[] = image - reference[] = string I will test this.

  • 1

    I don’t know what you mean myfoto[] = Imagem - referencia[] = string. You can [Dit] the question and gather more information?

  • Did you get what you wanted? You want to see if there’s n elements on the page or if they have value assigned?

  • I couldn’t. I want to compare if the arrays are equal. (Same amount)

  • And you can’t compare? Some error in the console? or Ubmit is not canceled?

  • I’ll test it better tomorrow and give you the feedback, I’ve tangled with something else, thanks for while @Sergio

  • I could not compare the number of occurrences between the arrays. For now I decided validating all fields so that they are not empty. $("#mail_form"). validate(); <input required>. Thanks for help @Sergio

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.