Format telephone and credit card numbers in AngularJS

前端 未结 17 2167
猫巷女王i
猫巷女王i 2020-11-30 18:29

Question one (formatting telephone number):

I\'m having to format a telephone number in AngularJS but there is no filter for it. Is there a way to u

17条回答
  •  被撕碎了的回忆
    2020-11-30 19:01

    I created an AngularJS module to handle this issue regarding phonenumbers for myself with a custom directive and accompanying filter.

    jsfiddle example: http://jsfiddle.net/aberke/s0xpkgmq/

    Filter use example:

    {{ phonenumberValue | phonenumber }}

    Filter code:

    .filter('phonenumber', function() {
        /* 
        Format phonenumber as: c (xxx) xxx-xxxx
            or as close as possible if phonenumber length is not 10
            if c is not '1' (country code not USA), does not use country code
        */
    
        return function (number) {
            /* 
            @param {Number | String} number - Number that will be formatted as telephone number
            Returns formatted number: (###) ###-####
                if number.length < 4: ###
                else if number.length < 7: (###) ###
    
            Does not handle country codes that are not '1' (USA)
            */
            if (!number) { return ''; }
    
            number = String(number);
    
            // Will return formattedNumber. 
            // If phonenumber isn't longer than an area code, just show number
            var formattedNumber = number;
    
            // if the first character is '1', strip it out and add it back
            var c = (number[0] == '1') ? '1 ' : '';
            number = number[0] == '1' ? number.slice(1) : number;
    
            // # (###) ###-#### as c (area) front-end
            var area = number.substring(0,3);
            var front = number.substring(3, 6);
            var end = number.substring(6, 10);
    
            if (front) {
                formattedNumber = (c + "(" + area + ") " + front);  
            }
            if (end) {
                formattedNumber += ("-" + end);
            }
            return formattedNumber;
        };
    });
    

    Directive use example:

    
    

    Directive code:

    .directive('phonenumberDirective', ['$filter', function($filter) {
        /*
        Intended use:
            
        Where:
            someModel.phonenumber: {String} value which to bind only the numeric characters [0-9] entered
                ie, if user enters 617-2223333, value of 6172223333 will be bound to model
            prompt: {String} text to keep in placeholder when no numeric input entered
        */
    
        function link(scope, element, attributes) {
    
            // scope.inputValue is the value of input element used in template
            scope.inputValue = scope.phonenumberModel;
    
            scope.$watch('inputValue', function(value, oldValue) {
    
                value = String(value);
                var number = value.replace(/[^0-9]+/g, '');
                scope.phonenumberModel = number;
                scope.inputValue = $filter('phonenumber')(number);
            });
        }
    
        return {
            link: link,
            restrict: 'E',
            scope: {
                phonenumberPlaceholder: '=placeholder',
                phonenumberModel: '=model',
            },
            // templateUrl: '/static/phonenumberModule/template.html',
            template: '',
        };
    }])
    

    Full code with module and how to use it: https://gist.github.com/aberke/042eef0f37dba1138f9e

提交回复
热议问题