sub ss_last_4 { my $n = shift; $n =~ tr/0-9//cd; # We can only work with numeric digits. # There are some basic constraints we can assure are met. die "$n cannot be a social security number.\n" if 9 != length($n) || $n =~ m/(^0{3})|(^\d{3}0{2})|(0{4}$)/ # A grouping of +zeros represents a fictitious SS number. || substr($n,0,3) == 666 # Prefixes that +== 666, or are >= 900 and <= 999 are reserved. || substr($n,0,3) >= 900; # Still alive, return the last four. return substr($n,-4,4); }

Just today a website rejected my phone number because I entered it without hyphens. Amazing how something that is really only a formatting convention gets baked into validation. Ok, for socials hyphens are supposed to be a requirement, but how many times have you visited websites where the hyphen handling is wonky? IMO it's easier to filter them out on input, and re-apply them on output.

Don't be tempted to use SSN::Validate (which could easily return the last four for you); in 2011 the US government made changes to how the first three digits are allocated, and the changes broke assumptions made in that module.


Dave


In reply to Re: masking SSN to last four by davido
in thread masking SSN to last four by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.