Skip to content

Instantly share code, notes, and snippets.

@hightemp
Created February 2, 2015 16:38
Show Gist options
  • Save hightemp/3862e50023b45b1b7035 to your computer and use it in GitHub Desktop.
Save hightemp/3862e50023b45b1b7035 to your computer and use it in GitHub Desktop.
Credit card number checker
function fnValidCreditCardNumber( objFormField )
{
var nSum = 0;
var nMultiplier = 1;
var nIndex;
var sDigit;
var nProduct;
var sCreditCardAccountNumber = objFormField.value;
var nLength = objFormField.value.length;
if( ( nLength < 13 ) || ( sCreditCardAccountNumber.length > 19 ) )
return(false);
for( nIndex = 0; nIndex < nLength; ++nIndex )
{
sDigit = sCreditCardAccountNumber.substring( (nLength-nIndex-1) , (nLength-nIndex) );
nProduct = parseInt(sDigit,10) * nMultiplier;
if( nProduct >= 10 ) nSum += ( nProduct % 10) + 1;
else nSum += nProduct;
if( nMultiplier == 1 ) ++nMultiplier;
else --nMultiplier;
}
if( ( nSum % 10) == 0 ) return(true);
else return(false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment