Format telephone and credit card numbers in AngularJS

前端 未结 17 2159
猫巷女王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:08

    Here is the way I created ssn directive which checks for the the pattern and I have used RobinHerbots jquery.inputmask

    angular.module('SocialSecurityNumberDirective', [])
           .directive('socialSecurityNumber', socialSecurityNumber);
    
    function socialSecurityNumber() {
        var jquery = require('jquery');
        var inputmask = require("jquery.inputmask");
        return {
            require: 'ngModel',
            restrict: 'A',
            priority: 1000,
            link: function(scope,element, attr, ctrl) {
    
                var jquery_element = jquery(element);
                jquery_element.inputmask({mask:"***-**-****",autoUnmask:true});
                jquery_element.on('keyup paste focus blur', function() {
                    var val = element.val();    
                    ctrl.$setViewValue(val);
                    ctrl.$render();
    
                 });
    
                var pattern = /^\d{9}$/;
    
                var newValue = null;
    
                ctrl.$validators.ssnDigits = function(value) {
                     newValue = element.val();
                    return newValue === '' ? true : pattern.test(newValue);    
                };
            }
        };
    }
    

提交回复
热议问题