Solved

Angular Telephone Filter without parenthesis

Posted on 2013-12-14
5
1,054 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now