Logical operators in form validation with php

Asked

Viewed 105 times

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 required. In this case, the user needs to fill in at least one of the fields in order to send the form.

<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 the understanding You can see, it doesn’t 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 right way to validate the form? If not, which would be the best?
  • How I could assemble the logic of the second attempt, correctly within php?

2 answers

4

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

Based only on this rule, 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 right way to validate the form? If not, which would be the best?

How to better validate the form:

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

Check the format of the phone 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 the format of the email with filter_var():

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

Note that I used trim() variables to remove any space at the beginning or end of the string. Use to avoid validation errors caused by extra spaces.

  • Just to complement the answer: it would be ideal to create filters and the same conditions also on the client side, via Javascript. That way when filling in the phone field, for example, it would look exactly the way you want it.

3

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

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

Browser other questions tagged

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