Validate a Textbox array in Javascript?

Asked

Viewed 121 times

0

Hello my doubt is the following, I have a js function like this:

function testeCampos(elemento) {

//Recebe lista de campos para valida��o 
var camposArray = new Array();
camposArray = elemento;

//Recebe campo armazenado no array
var campo, recFocu;

//Verifica se h� uma ocorrencia de erro 
var faltaPre = false

//varre o Array de Campos 
for (c = 0; c < camposArray.length; c++) {

    //cria Objeto com o metodo DOM
    campo = document.getElementById(camposArray[c]);

    if (campo.value == '') {

        campo.style.backgroundColor = "#eeee99";

        if (faltaPre != true) {
            faltaPre = true;
            recFocu = campo;
        }
    }
    else {
        campo.style.backgroundColor = "white";
    }
}

if (faltaPre == true) {
    alert('Campo com Preenchimento obrigat\xf3rio!!!');
    recFocu.focus();
    return false;
}
else {
    return true;
}
}

takes as parameter a textbox array and validates one by one

I am mounting the array on page_load page so:

TextBox[] aTextbox = { txtNome, TxtEndereco, TxtCEP, TxtBairro, TxtUF, TxtCidade, TxtTelefone, txtUsuario, TxtSenha };

and triggering the onClick event at Codebehind like this:

this.btnEnviar.Attributes.Add("onclick", "return testeCampos(aTextbox);");

inserir a descrição da imagem aqui

Is there an error in my code? because it is giving error in JS

CODE OF THE REGISTRATION PAGE

  • 1

    Your Textbox[] array should not be a string[] with field ids?

  • I did and the error continues string[] aTextbox = { txtNome.ID, Txtendereco.ID };

  • But I always send a textbox object, so I created the textbox array

  • Note: 0x800a1391 error - Javascript runtime error: 'aTextbox' is not set

  • 1

    Edit your question in more detail with the full page or view.

  • The idea is to scroll through textbox by textbox the first empty field it should give Focus in the blank and ready.

  • 1

    posted page, if it will not be only advocating... as for example aTextBox is a server side object... it was not written on the screen and so does not exist in the scope of javascript

  • Oops, sorry leandro updated with a git link has my registration page :(

  • Thanks leandro, get, I’ll leave my answer to members with doubt ;)

Show 4 more comments

1 answer

1


Good first you need to get the textbox on the front in case in HTML as an example:

 function testarCampos() {

         var arrayCampos = []; //Array de Objetos
         arrayCampos[0] = document.forms[0].NOME_DO_OBJETO
         arrayCampos[1] = document.forms[0].TxtEndereco;
     }

Then you move on to that function of yours

testeCampos(arrayCampos)

Well, your function turns true or false so you can compare

then do an if, so

         if (testeCampos(arrayCampos) == true) {
             return true;
         }else
         {
             return false;
         }

Complete Code

         function testarCampos() {

         var arrayCampos = [];
         arrayCampos[0] = document.forms[0].txtNome;
         arrayCampos[1] = document.forms[0].TxtEndereco;
         arrayCampos[2] = document.forms[0].TxtCEP;
         arrayCampos[3] = document.forms[0].TxtBairro;
         arrayCampos[4] = document.forms[0].TxtTelefone;
         arrayCampos[5] = document.forms[0].txtUsuario;
         arrayCampos[4] = document.forms[0].TxtSenha;

         //alert(arrayCampos[0].value);

         if (testeCampos(arrayCampos) == true) {
             return true;
         }else
         {
             return false;
         }


     }

Browser other questions tagged

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