Posts Tagged ‘dea’

In one of the module I came across to the situation where a physician needs to input their DEA number on a form and we have to validate the DEA Number, either using javascript or C#. I was provided with the algorithm from client and we had to write a javascript / c# function for the same. First I tried searching it on Google for some ready made stuff, but I could not found any, so written my own function to validate a DEA number. Here is the code snippet in both Javascript and C#:

C#
private bool DEAValidation(string DEANumber)
{
if (DEANumber != null && DEANumber != “” && DEANumber.Length == 9)
{
string strDEAFirstLetter = DEANumber.Substring(0, 1).ToUpper();
Response.Write(“strDEAFirstLetter: ” + strDEAFirstLetter);

if (strDEAFirstLetter != “A” && strDEAFirstLetter != “B” && strDEAFirstLetter != “M” && strDEAFirstLetter != “F”)
{
return false;
}

string strDEASecondLetter = DEANumber.Substring(1, 1).ToUpper();
string strValidChars = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
string strValidNums = “0123456789”;
if (strValidChars.IndexOf(strDEASecondLetter) < 0 && strValidNums.IndexOf(strDEASecondLetter) < 0)
{
return false;
}

int iThirdDigit = int.Parse(DEANumber.Substring(2, 1));
int iForthDigit = int.Parse(DEANumber.Substring(3, 1));
int iFifthDigit = int.Parse(DEANumber.Substring(4, 1));
int iSixthDigit = int.Parse(DEANumber.Substring(5, 1));
int iSeventhDigit = int.Parse(DEANumber.Substring(6, 1));
int iEightDigit = int.Parse(DEANumber.Substring(7, 1));
int iNinthDigit = int.Parse(DEANumber.Substring(8, 1));

int Total_3_5_7 = iThirdDigit + iFifthDigit + iSeventhDigit;
int Total_4_6_8 = iForthDigit + iSixthDigit + iEightDigit;

int FinalTotal = Total_3_5_7 + (2 * Total_4_6_8);

Response.Write(“FinalTotal: ” + FinalTotal.ToString());

if (iNinthDigit.ToString() != FinalTotal.ToString().Substring(FinalTotal.ToString().Length – 1, 1))
{
return false;
}

return true;
}
else
{
return false;
}
}

Javascript:


function DEAValidation(DEANumber)
{
try
{
if (DEANumber != null && DEANumber != “” && DEANumber.length == 9)
{

var strDEAFirstLetter = DEANumber.substr(0, 1).toUpperCase();

if (strDEAFirstLetter != “A” && strDEAFirstLetter != “B” && strDEAFirstLetter != “M” && strDEAFirstLetter != “F”)
{
return false;
}

var strDEASecondLetter = DEANumber.substr(1, 1).toUpperCase();

var strValidChars = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
var strValidNums = “0123456789”;

if (strValidChars.indexOf(strDEASecondLetter) < 0 && strValidNums.indexOf(strDEASecondLetter) < 0)
{
return false;
}

var iThirdDigit = parseInt(DEANumber.substr(2, 1));
var iForthDigit = parseInt(DEANumber.substr(3, 1));
var iFifthDigit = parseInt(DEANumber.substr(4, 1));
var iSixthDigit = parseInt(DEANumber.substr(5, 1));
var iSeventhDigit = parseInt(DEANumber.substr(6, 1));
var iEightDigit = parseInt(DEANumber.substr(7, 1));
var iNinthDigit = parseInt(DEANumber.substr(8, 1));

var Total_3_5_7 = iThirdDigit + iFifthDigit + iSeventhDigit;
var Total_4_6_8 = iForthDigit + iSixthDigit + iEightDigit;

var FinalTotal = Total_3_5_7 + (2 * Total_4_6_8);

if (iNinthDigit != FinalTotal.toString().substr(parseInt(FinalTotal.toString().length) – 1,1))
{
return false;
}

return true;

}
else
{
return false;
}
}
catch(err)
{
return false;
}
}