Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Checks if the input Credit Card number is a valid ...
Author:
Philip McGeehan
E-mail:
Click to e-mail author
Submitted:
5/5/2003
Version:
VB6
Compatibility:
VB6
Category:
Mathematics
Views:
11063
Checks if the input Credit Card number is a valid one.
Declarations:
'You just need 3 command buttons & 4 text boxes
Code:
Private Sub Command1_Click() scardno = Text1 & Text2 & Text3 & Text4 If Not IsNumeric(Text1 & Text2 & Text3 & Text4) Then MsgBox "the card number contains letters", vbCritical, "Shit" End If Const MAX_DIGITS = 20 ' actually don't know any card using more than 16 digits Dim anDigits(1 To MAX_DIGITS) As Byte Dim nDigits As Long Dim ofsCurrentDigit As Long Dim ofsCurrentCharacter As Long Dim CurrentCharacter As String Dim Multiplier As Long Dim CheckSum As Long Dim DigitValue As Long Dim ValidDigits As String If Len(Trim$(scardno)) < 1 Then Result = False GoTo Exit_Point End If ValidDigits = "0123456789" For ofsCurrentCharacter = 1 To Len(scardno) CurrentCharacter = Mid$(scardno, ofsCurrentCharacter, 1) If InStr(1, ValidDigits, CurrentCharacter, vbBinaryCompare) Then nDigits = nDigits + 1 If nDigits > MAX_DIGITS Then Result = False GoTo Exit_Point End If anDigits(nDigits) = Val(CurrentCharacter) End If Next ofsCurrentCharacter CheckSum = anDigits(nDigits) For ofsCurrentDigit = nDigits - 1 To 1 Step -1 If Multiplier = 2 Then Multiplier = 1 Else Multiplier = 2 End If DigitValue = anDigits(ofsCurrentDigit) * Multiplier CheckSum = CheckSum + DigitValue If DigitValue > 9 Then CheckSum = CheckSum - 9 End If Next ofsCurrentDigit Result = ((CheckSum Mod 10) = 0) Exit_Point: IsValidCreditCardNumber = Result If Result = False Then MsgBox Text1 & "-" & Text2 & "-" & Text3 & "-" & Text4 & " is a FALSE card number", vbCritical, "Credit Card # " & Text1 & "-" & Text2 & "-" & Text3 & "-" & Text4 & " is FALSE" ElseIf Result = True Then MsgBox Text1 & "-" & Text2 & "-" & Text3 & "-" & Text4 & " is a VALID card number", vbInformation, "Credit Card # " & Text1 & "-" & Text2 & "-" & Text3 & "-" & Text4 & " is VALID" End If End Sub Private Sub Command2_Click() Text1 = "" Text2 = "" Text3 = "" Text4 = "" Text1.SetFocus End Sub Private Sub Command3_Click() End End Sub Private Sub Text1_Change() If Len(Text1) = 4 Then Text2.SetFocus Else End If End Sub Private Sub Text2_Change() If Len(Text2) = 4 Then Text3.SetFocus Else End If End Sub Private Sub Text3_Change() If Len(Text3) = 4 Then Text4.SetFocus Else End If End Sub Private Sub Text4_Change() If Len(Text4) = 4 Then Command1.SetFocus Else End If End Sub
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement