How to check variables with jQuery so that they are not sent empty?

Asked

Viewed 500 times

3

I am learning about jQuery and have already started making Ajax requests to register the information in the mysql database. I have a code that has several variables and I would like to ask how I can check at once if there is an empty variable. I was thinking about doing this with array but I don’t know how to use it. These variables will be captured from the form Submit. I ask for help!

    var empresa;        
    var username;       
    var ativado;            
    var tipousuario;        
    var senha;              
    var nome;               
    var cpf;                
    var cnpj;               
  • 1

    You can use if as seen [in this reply][1]. 
 
 
 [1]: http://stackoverflow.com/a/6003920

  • I would put them in an if and check them one by one by returning a single answer or do several if’s with a custom response for each certain variable?

  • 1

    checks the server side and returns the error message to show in ajax.

  • 1

    You can validate in JS, but inevitably you will need to validate ALSO with PHP, since JS may be disabled.

1 answer

1


Sometimes validation is done on the client side and the server side. There are always several ways to do this.

To do a customer side check you will need to search for element.value, element.checked depending on the type of element, and in the case of input type="radio" you have to go through them all and see if one of them is selected. You can make it a little easier by joining a class to these elements, but otherwise test like this:

var validado = true, erros = 0;
$('input, select, textarea').each(function () {
    var nomeTag = this.tagName.toLowerCase()
    if (nomeTag == 'select' || nomeTag == 'textarea') validado = !!this.value;
    else if (this.type == 'checkbox') validado = !!this.checked;
    else if (this.type == 'radio') validado = !!$('input').filter(function () {
        return this.checked
    })[0];
    else validado = !!this.value

    if (!validado) {
        this.addClass('incompleto');
        erros++;
    } else {
        this.removeClass('incompleto');
    }
});
if (erros) // validacao falhou!

If the erros don’t give 0 after this code then there are empty fields. I joined an optional part to give a class to the empty elements for user visibility.

On the PHP side all fields go within $_GET or $_POST, it depends on how you are using it. There you can do a simple check with a for cycle:

foreach ($post as $chave=>$valor) if (!$valor) die('O campo '.$chave.' está  vazio!');

There are more complex methods to check the content type. In these examples, and in the sequence of what you asked, this only checks if there are empty fields.

Until: this check does nothing to make the content safe, that is, this check must follow another that prepares the content to be inserted in the database.

Browser other questions tagged

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