Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Angular Telephone Filter without parenthesis

Posted on 2013-12-14
5
Medium Priority
?
1,221 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:Charles Baldo
  • 3
  • 2
5 Comments
 
LVL 83

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:Charles Baldo
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 83

Expert Comment

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

Accepted Solution

by:
leakim971 earned 2000 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:Charles Baldo
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

864 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