Checking for a Valid SSN example:
/+******************************************************************
* Program : SSN.exe
* Source : SSN.d
* Author : David L. 'SpottedTiger' Davis
* Created Date : 18.May.02 MS Visual Basic v6.0
* Modified Date : 19.Jun.04 Rewritten in D v0.92 and greatly changed the code
* : based on the infomation found below.
* : http://www.investigateanyoneonline.com/ssn-aboutnumbers.shtml
* Modified Date : 08.Jul.04 Compiled and Tested with dmd v0.95
* : 02.Oct.04 Compiled and Tested with dmd v0.102
* : 04.Jun.06 Compiled and Tested with dmd v0.160
* : 10.Jan.07 Compiled and Tested with dmd v1.0
* Requirements : std.conv, and std.string
* License : Public Domain
*******************************************************************
*
* To Compiled: C:\dmd\MKoD_ex>dmd SSN.d
+/
private import std.string;
private import std.conv;
private import std.stdio;
/+
' isValidSSN - Check for a valid Social Security Number value.
' Requirements : std.conv, std.string
'
' Validates the SSN xxx-yy-zzzz
' ^ ^ ^
' | | |
' Area --------------+ | |
' Group ----------------+ |
' Serial -------------------+
'
'
' Returns True if valid, elsewise False if invalid
'
'Example:
' If isValidSSN("512-64-2048" <,True>) Then ...
'
+/
bool isValidSSN
(
in char[] sSSNin,
in bool bIncUnassignedAreaNums = false
)
{
char[] sSSN = sSSNin;
uint uiArea = 0U;
uint uiGroup = 0U;
uint uiSerial = 0U;
sSSN = replace( sSSN, "-", "" );
sSSN = replace( sSSN, "\x20", "" );
if ( sSSN.length != 9 && !isdigits( sSSN ) )
return false;
uiArea = toUint( sSSN[ 0 .. 3 ] );
uiGroup = toUint( sSSN[ 3 .. 5 ] );
uiSerial = toUint( sSSN[ 5 .. 9 ] );
// Make current used and unused "Area" Numbers valid.
if ( bIncUnassignedAreaNums == true && uiArea > 649U )
{
// Invaild or Impossible SSNs
// Checking Range ( 650 .. 999 )
// 666 will never be issued, the "Mark-of-the-Beast" number.
if ( uiArea == 666U )
return false;
// 650 .. 699 unassigned for future use, but valid
else if ( uiArea >= 650 && uiArea <= 699 )
return true;
// 700 .. 728 Railroad workers thru 1963, now discontinued
else if ( uiArea >= 700 && uiArea <= 728 )
return false;
// 729 .. 799 unassigned for future use, but valid
else if ( uiArea >= 729 && uiArea <= 799 )
return true;
// 800 .. 999 not valid ever, used for a different program
else if ( uiArea >= 800 && uiArea <= 999 )
return false;
// According to the SSA these SSNs are used for Advertising Only
//if ( ( uiArea == 987 && uiGroup == 65) &&
// ( uiSerial >= 4320 && uiSerial <= 4329 ) )
// return false;
}
else
{
// Only the current "Area" numbers open for use are valid.
// Checking Range ( 1 .. 649 )
if ( uiArea < 1U || uiArea > 649U )
return false;
// Invalid Group
else if ( uiArea == 550 && uiGroup == 19 )
return false;
// Invalid Groups
else if ( uiArea == 586 )
{
switch ( uiGroup )
{
case 19, 29, 59, 79, 80, 81:
case 82, 83, 84, 85, 86, 87:
case 88, 89, 90, 91, 92, 93:
case 94, 95, 96, 97, 98, 99:
return false;
}
}
// These SSNs were used in Advertising, making them invalid.
// Falling within the valid SSN Range ( 002 .. 549 )
switch ( sSSN )
{
case "002281852":
case "042103580":
case "062360749":
case "078051120":
case "095073645":
case "128036045":
case "135016629":
case "141186941":
case "165167999":
case "165187999":
case "165207999":
case "165227999":
case "165247999":
case "189092294":
case "212097694":
case "212099999":
case "306302348":
case "308125070":
case "468288779":
case "549241889":
return false;
default:
}
}
// * The "Group" number can't be zero, there are more rules...
// but I'll need to find out more before trying to used them.
// * The "Serial" number can't be zero, otherwise all others are useable.
if ( uiGroup == 0U || uiSerial == 0U ) return false;
// If its make it this far, there's a very good chance that it's valid.
return true;
} // end bool isValidSSN( in char[], in bool = false )
/+
' isdigit - Checks a string to see if all it's characters
' are digits, and if so if returns true.
'
' Author : David L. 'SpottedTiger' Davis
' Date Created : 19.Jun.04
' Language : DigitalMars D v0.92 (Complies with v0.95)
'
' Requirements : std.string
'
+/
bool isdigits
(
in char[] s
)
{
foreach ( dchar c; s )
if ( find( digits, cast(char)c ) == -1 ) return false;
return true;
} // end bool isdigit( in char[] )
int main()
{
writefln("isValidSSN( \"000-45-6789\" )=%b ans=0", isValidSSN( "000-45-6789" ) );
writefln("isValidSSN( \"001-00-6789\" )=%b ans=0", isValidSSN( "001-00-6789" ) );
writefln("isValidSSN( \"600-23-0000\" )=%b ans=0", isValidSSN( "600-23-0000" ) );
writef("\n");
writefln("isValidSSN( \"651-99-7999\", true )=%b ans=1", isValidSSN( "651-99-7999", true ) );
writefln("isValidSSN( \"651-99-0023\" )=%b ans=0", isValidSSN( "651-99-0023" ) );
writefln("isValidSSN( \"666-99-0023\", true )=%b ans=0", isValidSSN( "666-99-0023", true ) );
writefln("isValidSSN( \"550-19-0023\" )=%b ans=0", isValidSSN( "550-19-0023" ) );
return 0;
} // end int main()
C:\dmd\MKoD_ex>..\bin\dmd SSN.d
C:\dmd\bin\..\..\dm\bin\link.exe SSN,,,user32+kernel32/noi;
C:\dmd\MKoD_ex>SSN
isValidSSN( "000-45-6789" )=0 ans=0
isValidSSN( "001-00-6789" )=0 ans=0
isValidSSN( "600-23-0000" )=0 ans=0
isValidSSN( "651-99-7999", true )=1 ans=1
isValidSSN( "651-99-0023" )=0 ans=0
isValidSSN( "666-99-0023", true )=0 ans=0
isValidSSN( "550-19-0023" )=0 ans=0
C:\dmd\MKoD_ex>