Solved

Angular Telephone Filter without parenthesis

Posted on 2013-12-14
5
1,086 Views
Last Modified: 2013-12-14
I am trying to filter a phone number
 <td colspan="2">{{listingDetails.BKRPhoneNum | tel}}</td>

This works but puts in parenthesis
(555) - 555-1212

I would need 555-555-1212

the phone number comes in as 5555551212

Thank you
0
Comment
Question by:charlesbaldo
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39719340
Do you really need to save it like 555-555-1212 ?
I think it's prefable to save without formating and apply formatting when displaying data
0
 

Author Comment

by:charlesbaldo
ID: 39719403
Sorry it is saved in database as 5555551212 that is what I have to work with.  I need to display as 555-555-1212.   The filter I have is displaying it as (555)-555-1212
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39719404
could you post your filter?
you just need to remove the parentheses : "(" + xxx + ")"
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39719406
check test page : http://jsfiddle.net/6VKCc/

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 10: // +1PPP####### -> C (PPP) ###-####
                country = 1;
                city = value.slice(0, 3);
                number = value.slice(3);
                break;

            case 11: // +CPPP####### -> CCC (PP) ###-####
                country = value[0];
                city = value.slice(1, 4);
                number = value.slice(4);
                break;

            case 12: // +CCCPP####### -> CCC (PP) ###-####
                country = value.slice(0, 3);
                city = value.slice(3, 5);
                number = value.slice(5);
                break;

            default:
                return tel;
        }

        if (country == 1) {
            country = "";
        }

        number = number.slice(0, 3) + '-' + number.slice(3);

        return (country + city + "-" + number).trim();
    };
});

var myCtrl = function($scope) {
    $scope.listingDetails = {
        BKRPhoneNum:"5555551212"
    }
}

Open in new window

0
 

Author Closing Comment

by:charlesbaldo
ID: 39719430
Thanks I made the rookie error thinking the filter tel was an angular built in filter.  I will locate it and remove the ()
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question