%
'This ASP script originally lived at http://evolvedcode.net/ for the
'original version of this script and a wide variety of other
'scripts, please visit the site.
Option Explicit
'Define the minimum and maximum input sizes since
' it seems the majority of credit cards and similar products
' use luhns algorythm
Const iSeq_Lower = 16
Const iSeq_Upper = 19
Function Luhn_CheckSum_Make( ByVal sInput )
'Code to generate a correct sequence from an incorrect sequence
Dim iSum, iPartial, i, bAlternate
'Prepare some guide text to allow us to fail closed
Luhn_CheckSum_Make = "Expected " & iSeq_Lower & "-" & iSeq_Upper & " characters"
If Len( sInput ) >= iSeq_Lower And Len( sInput ) <= iSeq_Upper Then
'Cycle through characters Len()-1 to 1
bAlternate = False
For i = 1 To Len(sInput) - 1
'Get the literal value of character X
iPartial = CByte(Mid(sInput, Len(sInput) - i, 1))
'Toggle methods on and off
If bAlternate Then
'Use current value as is
bAlternate = False
Else
'Modify value, take double the original value and divide it by ten,
' then add to this the double the value modulo ten, finally convert
' this number into an integer (whole number)
bAlternate = True
iPartial = Int((iPartial * 2) / 10) + ((iPartial * 2) Mod 10)
End If
'Incorporate this value into a total of all the characters being cycled
' through
iSum = iSum + iPartial
Next
'Store the original sequence minus the checksum digit
Luhn_CheckSum_Make = Left(sInput, Len(sInput) - 1)
'Calculate the checksum for this sequence and append it
' since the total has to be cleanly divisible by 10, if we
' take the running total and get the modulo of 10 from it,
' we can then figure out what the last number has to be to
' make the entire sequence cleanly divisible
Luhn_CheckSum_Make = Luhn_CheckSum_Make & Trim(CStr(10 - (iSum Mod 10)))
End If
End Function
Function Luhn_CheckSum_Test( ByVal sInput )
'Code to test if a sequence of numbers is correct according to luhns algorythm
Dim iSum, iPartial, i, bAlternate
'Fail closed
Luhn_CheckSum_Test = False
'Check size of input
If Len( sInput ) >= iSeq_Lower And Len( sInput ) <= iSeq_Upper Then
'Cycle through characters Len()-1 to 1
bAlternate = False
For i = 1 To Len(sInput) - 1
'Get the literal value of character X
iPartial = CByte(Mid(sInput, Len(sInput) - i, 1))
'Toggle methods on and off
If bAlternate Then
'Use current value as is
bAlternate = False
Else
'Modify value, take double the original value and divide it by ten,
' then add to this the double the value modulo ten, finally convert
' this number into an integer (whole number)
bAlternate = True
iPartial = Int((iPartial * 2) / 10) + ((iPartial * 2) Mod 10)
End If
'Incorporate this value into a total of all the characters being cycled
' through
iSum = iSum + iPartial
Next
'Add the checksum bit
iSum = iSum + CByte(Mid(sInput, Len(sInput), 1))
'If it isn't cleanly divisible by 10 it's not valid
If iSum Mod 10 = 0 Then
Luhn_CheckSum_Test = True
End If
End If
End Function
Function Luhn_IsValid( ByVal sNumber )
'Code to act as a wrapper for the two main functions we have
'Prepare the number
sNumber = Trim( CStr(sNumber) )
'Check the number is as expected
If Not IsNumeric( sNumber ) Then
'String was not a number
sNumber = vbNullString
ElseIf Len( sNumber ) < iSeq_Lower Or Len( sNumber ) > iSeq_Upper Then
'String was teh wrong size
sNumber = vbNullString
End If
'Check we still have a number to work with
If sNumber <> vbNullString Then
'Valid number so far, prepare some guidance text
Luhn_IsValid = "The checksum for this sequence appears to be "
'Check the number against luhns algorythm
If Luhn_CheckSum_Test( sNumber ) Then
'Valid checksum
Luhn_IsValid = Luhn_IsValid & "valid."
Else
'Invalid checksum
Luhn_IsValid = Luhn_IsValid & "invalid.
With the correct checksum the sequence is " & Luhn_CheckSum_Make( sNumber ) & "."
End If
Else
'No number / Invalid number, display the guidance text
Luhn_IsValid = "Enter the card number you'd like to check below."
End If
End Function
%>
| Luhn's Algorythm |
| <%= Luhn_IsValid( Request.Form("txtCC") ) %> |