Contact Form

1

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

What I've 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've been looking here and see google and supposedly for what I read should give. But so far compels me to have 10 digits, 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.">
    
asked by anonymous 21.03.2014 / 21:49

7 answers

2

You can do this in JQuery or pure Javascript. But it's a great practice to always validate the inputs in your system. So always validate in PHP, even if it has been validated in 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 the general flow it could look 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:

link
link link

    
22.03.2014 / 16:10
1

I would remove characters that are not integers before doing 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;
}
    
21.03.2014 / 21:56
0

Use smaller and no different, different may be greater or less.

 if (strlen($phone) < 9){
    echo 'informe um numero de 9 digitos';
 }
    
21.03.2014 / 21:54
0

Try this out

if(strlen($_POST["phone"])==9 && is_numeric($_POST["phone"])){
  echo 'Número valido';
}else{
  echo 'Número inválido';
}
    
21.03.2014 / 21:54
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.";
    
21.03.2014 / 22:01
0

Ever try to do this check on the form, before giving the POST? You could set a mask with javascript the way you want the user to tell the number.

link

    
22.03.2014 / 13:40
0

The code below should fit what you need

$phone ="912341234";
$phone = isset($phone[8]) && is_numeric($phone) ? $phone : null;
if ($phone ===null ){
  echo "invalido";
 } else{
   echo "valido";
 }

Worth reading link

link

    
22.03.2014 / 02:21