Validation PHP numeric field

Asked

Viewed 151 times

1

I have the following function:

function valida_valor2($str)
{
    $count = strlen($str);

    if (($count > 19) OR (!is_numeric($str))) {
        return "INVALIDO";
    }else {
        return $str;
    }
}

When I call her in my code returns INVALIDO in the following call: Where the variables $valor_iem9... = 0;

if ((valida_valor2($valor_iem9_f_a) == 'INVALIDO') || (valida_valor2($valor_iem9_f_b) == 'INVALIDO') || (valida_valor2($valor_iem9_f_c) == 'INVALIDO') || (valida_valor2($valor_iem9_f_d) == 'INVALIDO') || (valida_valor2($valor_iem9_f_e) == 'INVALIDO')) {
    echo "Campo IEM9 Pessoa Física inválido";
    exit;
}

Some light?

  • Try to use AND instead of OR, to verify that the two conditions are met.

  • You want to validate only nothing dot digits or trace right?

  • Yes. I want to enter digits.

  • The function itself I believe is right but I’m in trouble at the time I call.

  • Actually when you pass the '0' by the function it does not interpret as an 'int', convert the value to string before using in the function.

2 answers

1

To validate whether input (string) contains only digits use the function ctype_digit().

You can simplify the logic by joining these variables into an array and then applying the function valida_valor2() for each element with array_filter() in callback make the comparison if the value returned is INVALID.

Finally check that the sum of invalid items is greater than or equal to one.

function valida_valor2($str){
    $count = strlen($str);

    if (!ctype_digit("$str") || $count > 19) {
        return "INVALIDO";
    } else {
        return $str;
    }
}

//junta as variáveis em um array
$arr = array(
    $iem9_f_a = str_repeat(1, 5),
    $iem9_f_b = str_repeat(2, 19),
    $iem9_f_c = str_repeat(2, 19),
    $iem9_f_d = '123abc',
    $iem9_f_e =  1.99
);


$validacao = array_filter($arr, function($item){ return valida_valor2($item) == 'INVALIDO';});


if(array_sum($validacao)){
    echo 'existem erros <pre>';
    print_r($validacao);
}else{
    echo 'OK';
}
  • the error persists. I believe my problem is here: if ((valida_value2($value_iem9_f_a) == 'INVALIDO') || (valida_value2($value_iem9_f_b) == 'INVALIDO') || (valida_value2($value_iem9_f_c) == 'INVALIDO') || (valida_value2($value_f_d) == 'INVALIDO') || (valida_value2($value_f_f_f_e) == 'INVALIDO')) { echo "Field IEM9 Invalid Person"; Exit; }

  • @Freitas the problem is in the if and not in the function

0

Try something like that:

function valida_valor2($str)
{
    $count = strlen($str);

    if (($count > 19) OR (!is_numeric($str))) {
        return "INVALIDO";
    } else {
        return $str;
    }
}

if ((valida_valor2(strval($valor_iem9_f_a)) == 'INVALIDO')) {
    echo "Campo IEM9 Pessoa Física inválido";
    exit;
}
  • but I could group several variables in this if? because I have value_iem9_f_a, value_iem9_f_b, value_iem9_f_c, value_iem9_f_d

  • Yeah, the same way you did, just used the strval() at each value received

  • You managed to solve it this way?

  • It still keeps returning INVALIDO. if ((valida_value2(strval($value_iem9_f_a)) == 'INVALIDO') OR (valida_value2(strval($value_f_b)) == 'INVALIDO') OR (valida_value2(strval($value_f_c)) == 'INVALIDO') OR (valida_value2(strval($value_f_d)) == 'INVALIDO') OR (valida_value2(strval($value_f_f_f_e)) == 'INVALIDO')) { echo "Field IEM9 Invalid Person"; Exit; }

Browser other questions tagged

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