Logical operators in form validation with php

0

Good morning,

I have these fields in an html form and I'm trying to validate it with php. In my validation with php, I need only one of the fields (any one) to be mandatory. In this case, the user must fill out at least one of the fields so that the form can be sent.

<input type="email" name="email" id="oemail" placeholder="Digite seu E-mail">
<input type="tel" name="whats" id="whats" placeholder="Digite seu whatsapp" maxlength="15">
<input type="tel" name="telefone" id="telefone" placeholder="Digite seu telefone" maxlength="14">

I am doing the validation with the following code. (working)

if (empty($whats) OR strstr($whats, ' ')==false) {
    $erro = 1;
}
if ($erro != 0) {
    echo '<script type="text/javascript">window.location = "erro.php";</script>';
    exit;
}

I tried to use the following code, but as understanders can see, it does not work:

if (empty($whats) OR strstr($whats, ' ')==false) and (empty($telefone) OR strstr($telefone, ' ')==false) and (empty($email) OR strstr($email, ' ')==false) {
    $erro = 1;
}
if ($erro != 0) {
    echo '<script type="text/javascript">window.location = "erro.php";</script>';
    exit;
}

The questions are as follows:

  • For my problem, this is the most correct way to validate the form? If not, which one would be the best?
  • How would I get the logic of the second try correctly within php?
asked by anonymous 04.11.2016 / 12:12

2 answers

4
  

I need only one of the fields (any) to be required.

Based on this rule only, the code below will work:

if ( !empty($whats) || !empty($telefone) || !empty($email) ) {
    // válido, pelo menos um campo não está vazio
} else {
   // inválido
    echo '<script type="text/javascript">window.location = "erro.php";</script>';
    exit;
}
  

For my problem, this is the most correct way to validate the   form? If not, which one would be the best?

How to validate the form better:

The above validation works but there are some things you can do to make your validation more secure and useful.

Check the phone format with preg_match() :

If you have a mask validating phones on the front end you can validate the format in PHP to ensure that you are only receiving valid data. Example:

if ( !preg_match( '|\(\d{2}\)\s\d{4}\d?\-\d{4}|', trim($telefone) ) {
    // telefone inválido pois não está no formato
    // (99) 9999-9999 ou (99) 99999-9999
}

Check email format with filter_var() :

if ( ! filter_var( trim($email), FILTER_VALIDATE_EMAIL ) ) {
   // email inválido
}

Note that I've used trim() in the variables to remove any space at the beginning or end of the string. It's good to use to avoid validation errors caused by extra spaces.

    
04.11.2016 / 12:32
3

Another alternative is to use the function array_filter() it will check each item in the array, if it is empty it will not be returned, ie if there is at least one value it will be evaluated as true in if.

$itens = array($whats, $telefone, $email);
if(array_filter($itens)){
    echo 'algum valor foi preenchido';
}else{
    echo 'nada foi preenchido';
}
    
04.11.2016 / 12:38