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();
});
}
};
});