Contact Form

Asked

Viewed 175 times

1

I have this code to check if it is a numeric value and I have tried that the maximum value of digits is 9. I also tried with != or ==

What I tried:

Option 1

    //php verif phone
if(isset($_POST["phone"])){
   if (strlen($phone) != 9) {   
    if (!$phone) {
        $errors[] = "Indique um número válido.";
    }elseif ( !is_numeric( $phone )) {
        $errors[]= "Verifique novamente o número.";
    }
}

Option 2

    //php verif phone
if(isset($_POST["phone"]) != 9){    
    if (!$phone) {
        $errors[] = "Indique um número válido.";
    }elseif ( !is_numeric( $phone )) {
        $errors[]= "Verifique novamente o número.";
    }
}

Option 3

    //php verif phone
if(isset($_POST["phone"])){ 
    if (!$phone) {
        $errors[] = "Indique um número válido.";
    }elseif ( !is_numeric( $phone ) != 9) {
        $errors[]= "Verifique novamente o número.";
    }
}

I searched here and via google and supposedly for what I read should give. But so far it forces me to have 10 digits, and not 9 as is my goal.

<label for="phone"></label>
<input name="phone" type="tel" id="phone" size="9"
   value="" placeholder="Telemóvel"
   class="required digits" title="Verifique novamente o número.">
  • What happens that the code does not work, from an input example, tbm, if the phone is formatted will not be interpreted as number.

  • That field requires me to have 10 digits, otherwise it tells me to check the number.

7 answers

2

You can do this in pure Jquery or Javascript. But it’s a great practice for you to always validate inputs on your system. Then always validate in PHP, even if it has been validated in the Front-end before. To validate if you have a numeric type you can use the is_numeric function that returns a boolean.

<?php
    if(is_numeric($phone)){}
?>

To know the amount of characters you can use this code below:

<?php
    if (function_exists('mb_strlen'))
    {
        return mb_strlen($phone);
    }
    return strlen($phone);
?>

In general flow it could be like this:

<?php
if(isset($_POST))
{
    if(!is_numeric($phone))
        return false;

    if (function_exists('mb_strlen'))
    {
        return mb_strlen($phone) < 9;
    }

    return strlen($phone) < 9;

}
?>

Documentation links:

http://php.net/is_numeric
http://www.php.net/manual/en/function.mb-strlen.php
http://www.php.net/isset

  • Thanks for the tip. I have to deepen my knowledge in PHP. However the customer wants to add the 00351 to the number so it was solved. Only with 9 digits was not giving.

1

I would remove characters that are not integer before making this check. An example:

<?php

$phone = isset( $_POST["phone"] ) ? $_POST["phone"] : null;

function verificaDigitosTelefone( $telefone , $permitido = 9)
{
    return ( strlen( preg_replace('/[^0-9]/', '' , $telefone ) ) == $permitido );
}


if( empty($phone) || !verificaDigitosTelefone($phone) )
{
    echo 'Telefone inválido';
    die;
}

0

use smaller and not different, different may be larger or smaller.

 if (strlen($phone) < 9){
    echo 'informe um numero de 9 digitos';
 }

0

Try it this way

if(strlen($_POST["phone"])==9 && is_numeric($_POST["phone"])){
  echo 'Número valido';
}else{
  echo 'Número inválido';
}

0

A simple three-line solution using ternary operator would be:

$phone = (isset($_POST['phone'])) ? (is_numeric($_POST['phone'] && strlen($_POST['phone']) >= 9) ? $_POST['phone'] : "";
if ($phone == "")
  echo "Número inválido.";

0

0

Browser other questions tagged

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