Reputation: 9738
I am trying to create a filter, which matches the number format xxx-xx-xxxx
ie. 123-12-1234.
angular.module('ng').filter('tel', function () {
return function (tel) {
if (!tel) { return ''; }
var value = tel.toString().trim().replace(/^\+/, '');
if (value.match(/[^0-9]/)) {
return tel;
}
var country, city, number;
switch (value.length) {
case 9: // +1PPP####### -> C (PPP) ###-####
country = 1;
city = value.slice(0, 3);
number = value.slice(4);
break;
default:
return tel;
}
if (country == 1) {
country = "";
}
number = number.slice(0, 2) + '-' + number.slice(3);
return (country + " " + city + "-" + number).trim();
};
});
function Ctrl($scope){
$scope.phoneNumber = 111223333;
}
Can somebody help me in this?
Upvotes: 1
Views: 1988
Reputation: 100175
do you mean::
...
switch (value.length) {
case 9: // +1PPP####### -> C (PPP) ###-####
country = 1;
city = value.slice(0, 3);
number = value.slice(3);
break;
default:
return tel;
}
if (country == 1) {
country = "";
}
number = number.slice(0, 2) + '-' + number.slice(2);
return (city + "-" + number).trim();
...
Demo:: fiddle
Upvotes: 1
Reputation: 1348
Like this? http://jsfiddle.net/4P7wD/1/
Change one digit.
number = number.slice(0, 2) + '-' + number.slice(1);
UPDATED
Hope it is what you need: http://jsfiddle.net/4P7wD/2/
Upvotes: 1