m7864 m7864 - 1 year ago 585
AngularJS Question

angularjs: mask the first five digits of SSN

I'm trying to mask the first 5 digits of SSN using a filter which should look something like this


What I came up with so far:

// <td>{{SocialSecurityNumber | ssn}}

angular.module('ng').filter('ssn', function () {
return function (ssn) {
if (!ssn) {
return '';

var value = ssn.toString().trim().replace(/^\+/, '');

if (value.match(/[^0-9]/)) {
return ssn;

return (ssn.slice(0, 3).replaceWith('*') + '-' + ssn.slice(4, 5).replaceWith('*') + '-' + ssn.slice(4)).trim();

Answer Source

Strings have no .replaceWith function in JavaScript. You can use .replace, though.

I'm not sure what value.match(/[0-9]/) is supposed to do either. It seems like you can just remove it. This will return true if the value has even one digit, which it should anyway. Perhaps you mean /[^0-9]/

return "XXX-XX-" + ssn.slice(5);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download