Skip to content

Instantly share code, notes, and snippets.

@jhonnrodr
Created April 15, 2015 20:41
Show Gist options
  • Save jhonnrodr/617917b1a96787758d2d to your computer and use it in GitHub Desktop.
Save jhonnrodr/617917b1a96787758d2d to your computer and use it in GitHub Desktop.
Format and valid SSN number
<?php
/*
PHP Library - Social Security Number-related functions
Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
This work is licensed under the Creative Commons Attribution License. To view
a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
94305, USA.
*/
// Format a Social Security Number.
function formatSSN($ssn)
{
if (eregi("^(\d{3})\-?(\d{2})\-?(\d{4})$", $ssn))
{
return eregi_replace("^(\d{3})\-?(\d{2})\-?(\d{4})$", "$1-$2-$3", $ssn);
}
else
{
die("Input cannot be formatted as a social security number.");
}
}
// Validate a Social Security Number.
function isValidSSN($ssn)
{
$result = eregi("^\d{3}\-?\d{2}\-?\d{4}$", $ssn) ? true : false;
/*
None of the digit groups can be all zeros.
Area number 666 is unassigned.
Numbers from 987-65-4320 to 987-65-4329 are reserved for use in advertisements.
Many SSNs have been invalidated by use in advertising.
*/
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)
{
$result = false;
}
// Numbers above 772 are currently unassigned.
if (substr($ssn, 0, 3) > 772)
{
$result = false;
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment