Solved

Angular Telephone Filter without parenthesis

Posted on 2013-12-14
5
1,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: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 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: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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery on click requires double click 6 33
Increase counter and attr inside a while loop 15 37
Two decimal 5 29
How do I split a variable to newline 2 18
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

733 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