Validate phone number with optional ninth digit

4

How do I validate my phone number with the optional ninth digit? in the following formats:

DDD + 8 digits = (21) 9876-5432

DDD + 9 digits = (21) 98765-4321

    
asked by anonymous 19.11.2016 / 05:45

4 answers

7

Since 2013 the phones with DDD 21 now have the ninth digit, I do not see why being optional, if you are saying that the entry can be landline as much as cell phone I recommend you do not do this, it will give a lot of headache

But if it's what you want you can do it like this:

^\(\d{2}\) 9?[6789]\d{3}-\d{4}$

Using the 9? within the regex the number 9 is optional, but still identifies if it is a cell, if it starts with 99, 98, 97 and 96 or 9, 8, 7 and 6, because within the regex it has this [6789]

Looking like this:

<?php
$celular = '(21) 98765-4321';

if (preg_match('#^\(\d{2}\) 9?[6789]\d{3}-\d{4}$#', $celular) > 0) {
     echo 'Validou';
} else {
     echo 'Não validou';
}

How to validate cell phones with and without the ninth digit

Note that cell phones start with 96, 97, 98, and 99 (or 6, 7, 8, and 9 if you do not have the ninth digit) as I mentioned above and conform to Anatel these will be the DDDs that will have the ninth digit:

The Anhanguera Resolution # 553/2010 determined the implementation of the ninth digit in all of Brazil.

  • DDD 11 (city of São Paulo and metropolitan region) won the ninth digit on July 29, 2012.

  • On August 25, 2013, it was the turn of DDD 12, 13, 14, 15, 16, 17, 18 and 19 (remainder of the state of São Paulo).

  • On October 27, 2013, the numbers of DDDs 21, 22 and 24 (state of Rio de Janeiro), and 27 and 28 (state of Espírito Santo) were changed. >

  • On November 2, 2014, the numbers of DDDs 91, 93 and 94 (state of Pará), 92 and 97 (state of Amazonas), 95 (state of Roraima), 96 Amapá), 98 and 99 (state of Maranhão).

  • By December 31, 2015, the numbers of DDDs 31, 32, 33, 34, 35, 37 and 38 (state of Minas Gerais), 71, 73, 74, 75 and 77 will be changed. (State of Rio Grande do Norte), 85 (State of Rio Grande do Norte), 85 (State of Rio Grande do Norte), 79 (State of Sergipe), 81 and 87 (state of Pernambuco), 82 Ceará), and 86 and 89 (state of Piauí).

  • By December 31, 2016, the numbers of DDDs 41, 42, 43, 44, 45 and 46 (state of Paraná), 47, 48 and 49 (state of Santa Catarina), 51, (State of Mato Grosso), 67 (state of Rio Grande do Sul), 61 (Federal District), 62 and 64 (state of Goiás), 63 (state of Tocantins), 65 and 66 Mato Grosso do Sul), 68 (state of Acre) and 69 (state of Rondônia).

In other words, for validation to be more precise we need a more "lean" RegEx, in case I suggest something like:

^(\((11|12|13|14|15|16|17|18|19|21|22|24|27|28|91|92|93|94|95|81|82|83|84|85|86|87|31|32|33|34|35|37|38|71|73|74|75|77|79|61|62|63|64|65|66|67|68|69|49|51|53|54|55)\) 9|\((?!11|12|13|14|15|16|17|18|19|21|22|24|27|28|91|92|93|94|95|81|82|83|84|85|86|87|31|32|33|34|35|37|38|71|73|74|75|77|79|61|62|63|64|65|66|67|68|69|49|51|53|54|55)\d{2}\) )[6789]\d{3}\-\d{4}$

It got a little long, simplified to explain how it works, it would look something like this:

^(\((11|21)\) 9|\((?!11|21)\d{2}\) )[6789]\d{3}\-\d{4}$
     ^        ^    ^                 ^     ^
     1        2    3                 4     5
  • Check to see if it starts with DDDs from the ninth-digit list
  • Check if the number starts with ninth digit if it is one of the DDDs in the list
  • Checks if the number does not start with the DDDs in the list and should also not have the ninth digit
  • Verify that the phone number starts with 6, 7, 8, 9, 96, 97, 98 or 99
  • Checks if the remainder after the prefix is numero
  • In this second example, you only accept DDDs 11 and 21 for the phone with the ninth digit and if you do not have the ninth digit it only accepts DDDs 21 and 11.

    A check function would look like this:

    <?php
    function validarCelular($celular)
    {
        static $regex;
    
        if ($regex === null) {
            //Coloquei em um array para identificar melhor
            $ddds = implode('|', array(
                11, 12, 13, 14, 15, 16, 17, 18, 19,
                21, 22, 24, 27, 28,
                91, 92, 93, 94, 95,
                81, 82, 83, 84, 85, 86, 87,
                31, 32, 33, 34, 35, 37, 38,
                71, 73, 74, 75, 77, 79,
                61, 62, 63, 64, 65, 66, 67, 68, 69,
                49, 51, 53, 54, 55
            ));
    
            //Gera a regex
            $regex = '#^(\((' . $ddds . ')\) 9|\((?!' . $ddds . ')\d{2}\) )[6789]\d{3}-\d{4}$#';
        }
    
        return preg_match($regex, $celular) > 0;
    }
    
    $celular = '(21) 98765-4321';
    
    if (validarCelular($celular)) {
         echo 'Validou';
    } else {
         echo 'Não validou';
    }
    
    • PHP example: link
    • Regex example: link I did simplified and with several numbers to understand the match within the field, note that it does not take the disabled
    20.11.2016 / 02:41
    6

    Friend, some time ago I used the following regex to validate:

    (\(?\d{2}\)?) ?9?\d{4}-?\d{4}
    

    I added the same to the regexr community: link

    I hope I have helped.

    Q: You can enter this regex in the input pattern attribute for the phone to do a client-side validation.

    Edit:

    Example of using regex with the preg_match() function:

    <?php
    $telefone = '(21) 98765-4321';
    if (preg_match('/(\(?\d{2}\)?) ?9?\d{4}-?\d{4}/', $telefone)) {
        echo "O telefone passou na validação";
    } else {
        echo "O telefone não passou na validação";
    }
    ?>
    

    You can find more examples on the preg_match () function page in the official PHP documentation:

    link

        
    19.11.2016 / 12:19
    2

    The answer of Renoir Dos Reis is with some errors, I made a few changes

    ^((\(\d{2}\))|\d{2}) ?9?\d{4}-?\d{4}$
    

    o ^ at the beginning means that the expression must start there, and $ means that it must end there. The way I would accept any string that contained a phone in the middle, would come out as correct.

    sfsjkahfsjka2199999999vsjkv

    Include | \ d {2} and removed ? from parentheses. | means or then you accept both (21) 9999-9999 and 21 9999-9999 , but does not accept (219999-9999 or 21) 9999-9999 . As it was before the parentheses were individually optional, now if you have one of the parenthesis you must necessarily have the other.

        
    23.10.2017 / 15:10
    0

    You can use the following rule:

    $exp_regular = '^(\(11\) [9][0-9]{4}-[0-9]{4})|(\(1[2-9]\) [5-9][0-9]{3}-[0-9]{4})|(\([2-9][1-9]\) [5-9][0-9]{3}-[0-9]{4})$';
    $ret = preg_match($exp_regular, $phone);
    
    preg_match($exp_regular, '(11) 92222-2222');
    preg_match($exp_regular, '(43) 8222-2222');
    
        
    19.11.2016 / 12:45