Re: Validation class method (ssn)

From: Date: Thu, 06 Jun 2002 21:07:01 +0000
Subject: Re: Validation class method (ssn)
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-6761@lists.php.net to get a copy of this message
On Thu, 6 Jun 2002, Tim Gallagher wrote: > At 02:48 PM 6/6/2002 -0500, Brent Cook wrote: > > >Here is a script to convert the list of high SSN numbers from the SSA > >website into a PHP array. Well, it actually generates the code for a PHP > >array, which could be loaded by a validation function very quickly. I'm > >working on the validator now. ... > > > Here is a function to determine the ssnGroup number from a given group > And here's the code to do the full validation ;) Note, I assume here that the array $high_ssn_list is defined in a separate file, containing key=>value pairs of area=>highgroup. The script I supplied earlier generates the array (though the name changed). <?php class Validate { function ssnGroup($groupNumber) { if ($groupNumber < 10) { if (!($groupNumber%2)) { return 3; } else { return 0; } // end if } elseif ($groupNumber > 9) { if (!($groupNumber%2)) { return 2; } else { return 1; } // end if } // end elseif } // end func /** * checks if a Social Security Number is valid * needs the first three digits and first two digits and the * final four digits as separate integer parameters */ function ssn ($area, $group, $serial, &$high_ssn_list) { // perform trivial checks // no field should contain all zeros // area should never be above 770 if (!($area && $group && $serial && ($area <= 770))) return FALSE; // check if this area has been assigned yet if (!($high_group = $high_ssn_list[$area])) return FALSE; $high_group_range = Validate::ssnGroup($high_group); $group_range = Validate::ssnGroup($group); // if the assigned range is higher than this group number, we're OK if ($high_group_range > $group_range) return TRUE; else // if the assigned range is lower than the group number, that's bad if ($high_group_range < $group_range) return FALSE; else // we must be in the same range, check the actual numbers if ($high_group >= $group) return TRUE; else return FALSE; } } // some testing code // setup the $high_ssn_list array include 'high_ssn_list.php'; $test_numbers = array ('123-23-2313','463-91-1234','123-45-6789','000-11-1111', '111-00-1231','123-23-0000', '999-12-3452', '743-99-2311'); foreach ($test_numbers as $number) { list ($area, $group, $serial) = explode('-',$number); echo $number.' is '; if (Validate::ssn(intval($area), intval($group), intval($serial), $high_ssn_list)) echo "Valid\n"; else echo "Invalid\n"; } ?>

« previous php.pear.dev (#6761) next »