Last active
May 12, 2020 21:00
-
-
Save Kryptonit3-zz/7b6bff5abab4a62e2b796a0e5a9ab94e to your computer and use it in GitHub Desktop.
Validate Social Security Number SSN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Validate SSN - must be in format AAA-GG-SSSS or AAAGGSSSS | |
* | |
* @param $ssn | |
* @return bool | |
*/ | |
function validate_ssn($ssn) { | |
$ssnTrimmed = trim($ssn); | |
// Must be in format AAA-GG-SSSS or AAAGGSSSS | |
if ( ! preg_match("/^([0-9]{9}|[0-9]{3}-[0-9]{2}-[0-9]{4})$/", $ssnTrimmed)) { | |
return false; | |
} | |
// Split groups into an array | |
$ssnFormatted = (strlen($ssnTrimmed) == 9) ? preg_replace("/^([0-9]{3})([0-9]{2})([0-9]{4})$/", "$1-$2-$3", $ssnTrimmed) : $ssnTrimmed; | |
$ssn_array = explode('-', $ssnFormatted); | |
// number groups must follow these rules: | |
// * no single group can have all 0's | |
// * first group cannot be 666, 900-999 | |
// * second group must be 01-99 | |
// * third group must be 0001-9999 | |
foreach ($ssn_array as $group) { | |
if ($group == 0) { | |
return false; | |
} | |
} | |
if ($ssn_array[0] == 666 || $ssn_array[0] > 899) { | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment