Skip to content

Instantly share code, notes, and snippets.

@micah1701
Last active January 25, 2023 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micah1701/45b0c9bd78178302801f8796649af0fe to your computer and use it in GitHub Desktop.
Save micah1701/45b0c9bd78178302801f8796649af0fe to your computer and use it in GitHub Desktop.
Validate and sanatize a user entered DEA Registration ID with PHP
/**
* Validate and clean a DEA Registration ID
* @param string $enteredValue user supplied DEA ID
* @param string $lastname OPTIONAL extended validation requires first letter of users last name to match corresponding character in ID
*
* @return string|bool returns sanitized alphanumeric DEA Registration ID or FALSE
*/
function cleanDEA(string $enteredValue, string $lastname = '') {
//if a " Supervisee Identifier" was supplied, just ignore it
$parts = explode("-",$enteredValue);
$deanumber = preg_replace( '/[\W]/', '', $parts[0]); // also strip anything that's not a letter or number
$dea = strtoupper($deanumber);
//value should be 9 characters
if(strlen($dea) != 9){
return false;
}
//First character must be a letter, but not "I","O","Q","V","W","Y", or "Z"
$cannotStartWith = ["I","O","Q","V","W","Y","Z"];
if(in_array($dea[0],$cannotStartWith) || is_numeric($dea[0])){
return false;
}
//Second character is a letter (from registrants last name) OR the number "9" if registered as a business
if(is_numeric($dea[1]) && $dea[1] !== 9){
return false;
}
elseif($lastname != '' && strtoupper($lastname[0]) !== $dea[1]){
return false;
}
$last7 = substr($dea,-7);
if(!is_numeric($last7)){
return false;
}
$numberParts = str_split($last7);
$odds = $numberParts[0] + $numberParts[2] + $numberParts[4]; // add the odd numbers
$evens = ($numberParts[1] + $numberParts[3] + $numberParts[5]) * 2; // add the evens and double
$together = ($odds + $evens) % 10; //get last digit of the odds/evens combined
return ($together == $numberParts[6]) ? $dea : false;
}
@micah1701
Copy link
Author

micah1701 commented May 5, 2021

usage:

<?php

if( !cleanDEA($_POST['myDEAnumber'])){
    echo "The DEA registration ID you supplied does not appear to be correctly formated";
}
else {
    echo "Thank you for confirming your DEA registration ID.  Enjoy your controlled substances";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment