Phone validation C#

Asked

Viewed 257 times

0

Some time ago I searched here validation for the phone number that only accepted the format type (11) 98162-7844 or (11) 8162-7844, however I did not find a validation for the "existing" or non-sequential number, eg:

(11) 9999-9999
(99) 9999-9999
(12) 1212-1212

any idea?

public static void Main()
{

    string Numero = "(11) 99999-9999";

    Regex regex = new Regex(@"^(\([0-9]{2}\))\s([9]{1})?([0-9]{4})-([0-9]{4})$");
    Match match = regex.Match(Numero);

    if (match.Success)
        Console.WriteLine("Este telefone é válido");
    else
        Console.WriteLine("Número inválido");
}
  • I write that I researched and yet always has kkk Dude you tested the code of this post there? boot a Phone (99) 9999-9999 will give valid phone, if you prove to me that there is this phone, then we can consider duplicate.

  • Sorry, I hadn’t been paying attention to the "existing number". regex only checks the format, to know if a phone really exists, only consulting specific services (that maybe the operators or Anatel provide, but then I no longer know). Of course you can change the regex to only have valid Ddds ((11|14|15|...)), but to find out if the number exists there is no more than regex...

  • And the rule for sequential numbers has to be very well defined so as not to exclude valid numbers. For example, 98765-1234 cannot be valid? If silly there are numbers like this - I did not call to test :-) but nothing prevents that there are such numbers... What could having is a simpler rule, like all the same digits (or "the same majority", and defining the value of "majority" is also arbitrary). Anyway, that’s it, only with regex you might not be able to do what you want (unless you create some more simplified arbitrary criteria)

  • I think it’s simpler than that, we need it would be sequential, it could be up to ddd 11 but I can’t accept 9999-9999 or 8888-888 or 1234-5678

  • 4

    @Voltz but these are valid numbers. It makes no sense to block. It is wanting to solve the problem in the wrong way, it is a case of XY problem. The only rule for cellular in Brazil is 1o digit to be 9.

  • How is this valid? 11 99999-9999?

  • 1

    Perfectly, why wouldn’t it be? That of a current coworker is 19 9 9999-9*** (I omitted the 3 last digits for privacy, and tb has repetition in them - but could end in 999 without any problem) - If you want to validate a phone, send an SMS (or robocall for fixed) and make the user confirm the code received (this is what I do on some systems).

  • 1

    To avoid repeated numbers, you can adapt that solution (changing the format to phone, obviously - but as Bacco said, it can still be valid), but to check sequentially it is easier not to use regex. Regular expressions work with text (or 9 is treated as the character corresponding to digit 9, not the numeric value itself) and checking that the next is equal to "previous + 1" is much easier to do with a programming language and its string and number functions, than with regex :-)

  • 2

    Even the same Voltz that you went through last (11) 9999...... exists and works, the way you failed to test (poor owner now...)

  • 1

    @Bacco I even thought about doing this test, but I was afraid of actually existing the number :-)

  • 1

    In that link have the formatting rules for Brazilian phone numbers.

Show 6 more comments
No answers

Browser other questions tagged

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