CFLib.org – Common Function Library Project

isMod10(number)

Last updated October 2, 2008
Download UDF

author

Scott Glassbrook Scott Glassbrook

Version: 3 | Requires: ColdFusion 5 | Library: UtilityLib

 
Rated 3 time(s). Average Rating: 3.3

Description:
Checks a string against the Luhn Algorithm. Also known as the mod10 algorithm. this function can be used to check the validity of credit card numbers, Canadian social insurance numbers, and any other number as well.

Return Values:
Returns a boolean.

Example:

<cfoutput>
<cfif IsMod10("4000300020001000")>
This string has passed the mod10 check
<cfelse>
This string has failed the mod10 check
</cfif>
</cfoutput>

Parameters:

Name Description Required
number String to check. Yes

Full UDF Source:

<cfscript>
/**
* Checks to see whether a string passed to it passes the Luhn algorithm (also known as the Mod10 algorithm)
* V2 update by Christopher Jordan cjordan@placs.net
* V3 update by Peter J. Farrell (cjordan@placs.netpjf@maestropublishing.com)
*
* @param number      String to check. (Required)
* @return Returns a boolean.
* @author Scott Glassbrook (cjordan@placs.netpjf@maestropublishing.comcflib@vox.phydiux.com)
* @version 3, October 2, 2008
*/

function isMod10(number) {
var nDigits = Len(arguments.number);
var parity = nDigits MOD 2;
var digit = "";
var sum = 0;
var i = "";

for (i=0; i LTE nDigits - 1; i=i+1) {
digit = Mid(arguments.number, i+1, 1);
if ((i MOD 2) EQ parity) {
digit = digit * 2;
if (digit GT 9) {
digit = digit - 9;
}
}
sum = sum + digit;
}

if (NOT sum MOD 10) return TRUE;
else return FALSE;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
2 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson