Regular expression for Telephones only with numbers and DDD C#

Asked

Viewed 2,522 times

1

I would like a help to create a regular expression in C# that validates number of phones with DDD but no dots and dashes, only numbers. Would be in XX12345678 and XX 123456789 format.

  • How about: .. ......(.(.(.)?)?)? Space only to facilitate reading, not real. And in place of . you can put [0-9]

2 answers

4

No real need to validate DDD, only digits remain :

\d{10,11}

var list = [
  '5112345678',
  '61123456789',
  '1112345678',
  '21123456789',
];

for(var i in list){
  console.log(list[i], /\d{10,11}/.test(list[i]));
}

  • Has calls where still use 7 digits for phone, in some interior lost.

  • 1

    @Jeffersonquesado just do the calculation 7 digits (min) + 2 DDD = 9 then starts at 9 and the other 9 digits (max) +2 DDD = 11, so the range is {9,11}

1


  • Reading the answer worked, I only removed the trace and parentheses for my validation. Ficou desta forma: ^[1-9]{2}(?:[2-8]|9[1-9])[0-9]{3}[0-9]{4}$ Obrigada!

Browser other questions tagged

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