Regular expression for phones?

Asked

Viewed 8,821 times

1

I created a Directive to format phones with or without digit 9. Now I need a regular expression to validate whether the number format is correct on ng-pattern. I created a regular expression but I’m still not getting results if the number is valid or not.

How to do this ?

regular expression

^\(?\d{2}\)?[\s-]?[\s9]?\d{4}-?\d{4}$

html

<input type="text" 
       placeholder="Telefone"
       name="telemp" 
       ng-model="Empresa.telefone" 
       ui-telefone
       ng-required="true"
       ng-pattern="^\(?\d{2}\)?[\s-]?[\s9]?\d{4}-?\d{4}$">

Directive

var app = angular.module('starter');

app.directive('uiTelefone', function(){

    return {
        require: 'ngModel',
        link: function(scope, element, attr, ctrl){
            var _formatTelefone = function(telefone){

                //(99)9999-9999 - 13dig
                //(99)99999-9999 - 14dig
                telefone = telefone.replace(/[^0-9]+/g, "");                
                if(telefone.length > 0){
                    telefone = telefone.substring(-1,0) + "(" + telefone.substring(0);
                }
                if(telefone.length > 3){
                    telefone = telefone.substring(0,3) + ")" + telefone.substring(3);
                }
                if(telefone.length == 12){
                    telefone = telefone.substring(0,8) + "-" + telefone.substring(8);
                }else if(telefone.length >= 13){
                    telefone = telefone.substring(0,9) + "-" + telefone.substring(9,13);
                }

                return telefone;
            }

            element.bind('keyup', function(){
                ctrl.$setViewValue(_formatTelefone(ctrl.$viewValue));
                ctrl.$render();
            });

        }
    };

});

7 answers

3

I created a regular expression that identifies whether a string is a phone, taking into account the following cases:

  • telephones 0800
  • numbers of operators and services like 10315 and 190
  • telephones represented with or without parentheses
  • accepts carrier represented as 0xx11
  • phones with or without separators [ .-]
  • ignores phones started with 0 if there is no DDD (ex: 0999-9999 is not accepted, but 0xx11 9123-1234 is)

It got a little big and difficult to read humanly, but it attends my projects:

/^1\d\d(\d\d)?$|^0800 ?\d{3} ?\d{4}$|^(\(0?([1-9a-zA-Z][0-9a-zA-Z])?[1-9]\d\) ?|0?([1-9a-zA-Z][0-9a-zA-Z])?[1-9]\d[ .-]?)?(9|9[ .-])?[2-9]\d{3}[ .-]?\d{4}$/gm

You can see the expression in use at this link.

1

Buddy, take a look at this:

^(\(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})$
  • Oops, it hasn’t worked yet !

1


I managed to make it, eh much simpler than I imagined.

did so.

ng-pattern="/^\([1-9]{2}\)[0-9]{4,5}-[0-9]{4}$/"

that video helped me a lot.

0

Friend, It’s pretty much what you put up, however, at the angle, you have to start and finish with the / . would be:

ng-pattern="/^\(\d{2}\)\d{4,5}-\d{4}$/"

0

Use this one with 9 validation and space after ddd

ng-pattern="^\([1-9]{2}\) [9]{1}[0-9]{4,5}-[0-9]{4}$"

0

Personal I am using angular 8.
All the examples didn’t work for me. After so much research I discovered that I had to put \\ before the strings.
I am using the regex of this site here: https://social.msdn.microsoft.com/Forums/pt-BR/07656880-7c55-4a18-b208-8f4d2c3d69b1/regex-para-numero-de-telefone-comum-ou-celular-com-esse-formato-11-12341234-ou-11-123451234?forum=aspnetpt

In the example of the site this so:

^(\([0-9]{2}\))\s([9]{1})?([0-9]{4})-([0-9]{4})$

I put it there \\ and funfou:

^(\\([0-9]{2}\\))\\s([9]{1})?([0-9]{4})-([0-9]{4})$

-1

If you are using the same field for mobile and landline, just switch to: ^[1-9]{2}[0-9]{4,5}[0-9]{4}$

  • It’s exactly the expression of the other answers, isn’t it? I don’t understand the difference.

  • No, this modification I made allows you to use the field for any phone number (fixed/mobile). The previous allows only mobile.

Browser other questions tagged

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