Regular expression for phones?

1

I created a directive to format phones with or without the digit 9. Now I need a regular expression to validate if the number format is correct in 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();
            });

        }
    };

});
    
asked by anonymous 05.01.2016 / 05:53

6 answers

0

I was able to do it, and it was much simpler than I imagined.

I did so.

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

This video helped me a lot.

    
05.01.2016 / 06:53
3

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

  
  • 0800 phones
  •   
  • numbers of carriers and services such as 10315 and 190
  •   
  • phones represented with or without parentheses
  •   
  • accepts operator represented as 0xx11
  •   
  • phones with or without separators [.-]
  •   
  • Ignore phones starting with 0 if you do not have DDD (ex: 0999-9999 is not accepted, but 0xx11 is 9123-1234)
  •   

It got a little big and hard to read humanly, but it suits 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

    
16.06.2016 / 20:22
1

Friend, 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})$
    
05.01.2016 / 06:03
0

Friend, It is practically what you put on and on, however, in the angular, you have to start and end with the /. would be:

ng-pattern="/^\(\d{2}\)\d{4,5}-\d{4}$/"
    
29.03.2016 / 21:58
0

Use this with validation of 9 and space after ddd

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

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

    
05.04.2018 / 23:04