Validate form with name [] and id equal

0

I have a dynamic form, where you can see several textarea , and by default, this textarea has the same id and the same name , but name is being sent as array % (name="nomecampo[]" ). I would like to know how I can validate all these textarea in javascript , because I need to check if these textarea has less than 10

The textarea looks like this:

<textarea class="form-control txta" rows="4" name="descr_icon[]" id="descri_icon" ></textarea>

And since the form is dynamic, one, none, or several textarea can appear like this, and I need to validate in javascript (need to be neat, I can not use jquery ) each% with% of this, but the validation is the same for all.

I found some solutions counting the index of textarea by textarea , but in my case name is passing name , so it did not work. What I found was something like this:

function validacao(nomecampo) {
    var total = document.getElementsByName(nomecampo);
    for(i = 0; i < total.lenght; i++) {
        if (document.getElementByName(i) < 10) {
            alert('Erro');
          return (false);
        }
    }
}

And in the function call:

<textarea name="camponome" id="camponome"></textarea>
<button onclick="validacao('camponome')"></button>

But as I said, this did not work in my case because I pass array to name ( array ).

Would anyone know of a possible solution?

    
asked by anonymous 02.08.2017 / 17:44

1 answer

1

The function call you have to pass exactly as it is in name.

<button onclick="validacao('descr_icon[]')">Validar campos</button>

In the function you have to go through the elements and check each element, picking up the element from the for index.

function validacao(nomeDosCampos) {
        var todosTextArea = document.getElementsByName(nomeDosCampos);
        for (var i = 0; i < todosTextArea.length; i++) {
            var textArea = todosTextArea[i];
            if (textArea.value.length < 10) {
                alert("Não é permitido menos de 10 caracteres");
                return;
            }
        }
 }
    
02.08.2017 / 18:06