Posted By

Scooter on 04/18/09


Tagged


Versions (?)

Social Security Numbers


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2009/04/social-security-numbers.html

Format and validate social security numbers. Be aware of the legal issues surrounding the collection of these private numbers.

  1. <?php
  2. /*
  3.   PHP Library - Social Security Number-related functions
  4.  
  5.   Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
  6.  
  7.   This work is licensed under the Creative Commons Attribution License. To view
  8.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  9.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  10.   94305, USA.
  11.   */
  12.  
  13. // Format a Social Security Number.
  14. function formatSSN($ssn)
  15. {
  16. if (eregi("^(\d{3})\-?(\d{2})\-?(\d{4})$", $ssn))
  17. {
  18. return eregi_replace("^(\d{3})\-?(\d{2})\-?(\d{4})$", "$1-$2-$3", $ssn);
  19. }
  20. else
  21. {
  22. die("Input cannot be formatted as a social security number.");
  23. }
  24. }
  25.  
  26. // Validate a Social Security Number.
  27. function isValidSSN($ssn)
  28. {
  29. $result = eregi("^\d{3}\-?\d{2}\-?\d{4}$", $ssn) ? true : false;
  30.  
  31. /*
  32.   None of the digit groups can be all zeros.
  33.   Area number 666 is unassigned.
  34.   Numbers from 987-65-4320 to 987-65-4329 are reserved for use in advertisements.
  35.   Many SSNs have been invalidated by use in advertising.
  36.   */
  37. if (eregi("^((000|666)\-?\d{2}\-?\d{4}|\d{3}\-?00\-?\d{4}|\d{3}\-?\d{2}\-?0000|987\-?65\-?432\d{1}|042\-?10\-?3580|062\-?36\-?0749|078\-?05\-?1120|095\-?07\-?3645|128\-?03\-?6045|135\-?01\-?6629|141\-?18\-?6941|165\-?(16|18|20|22|24)\-?7999|189\-?09\-?2294|212\-?09\-?(7694|9999|219\-?09\-?9999|306\-?30\-?2348|308\-?12\-?5070|468\-?28\-?8779|549\-?24\-?1889)$", $ssn)
  38. {
  39. $result = false;
  40. }
  41.  
  42. // Numbers above 772 are currently unassigned.
  43. if (substr($ssn, 0, 3) > 772)
  44. {
  45. $result = false;
  46. }
  47.  
  48. return $result;
  49. }
  50.  
  51. ?>

Report this snippet  

You need to login to post a comment.