How can I compare the number of Array occurrences in JavaScript dynamic fields before submitting form data. I need to check that both are the same. I can do only after sending with the count function of PHP , but I would like to know if there is how to do before sending, and if possible via JavaScript in>, since it is already validating the form. How to count these Array values before shipping?
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 trying unsuccessfully something like:
$(document).ready(function() {
$("#mail_form").submit( function() {
if(referencia.array.length != myfoto.array.length)
{
alert("Falta alguma coisa");
return false;
}
});
});