Credit card number validation

J

James

I have a function that will check the digits in a credit
card to see if there valid. But, I do not know where to
put the cose or what to do to make it work. The code is
as follows:

Function CheckCard(CreditCardNumber As String) As Boolean
Dim Counter As Integer, TmpInt As Integer
Dim Answer As Integer
Counter = 1
TmpInt = 0


While Counter <= Len(CreditCardNumber)


If (Len(CreditCardNumber) Mod 2) Then
TmpInt = Val(Mid$(CreditCardNumber, Counter,
1))


If Not (Counter Mod 2) Then
TmpInt = TmpInt * 2
If TmpInt > 9 Then TmpInt = TmpInt - 9
End If
Answer = Answer + TmpInt
Counter = Counter + 1
Else
TmpInt = Val(Mid$(CreditCardNumber, Counter,
1))


If (Counter Mod 2) Then
TmpInt = TmpInt * 2
If TmpInt > 9 Then TmpInt = TmpInt - 9
End If
Answer = Answer + TmpInt
Counter = Counter + 1
End If
Wend
Answer = Answer Mod 10
If Answer = 0 Then CheckCard = True
End Function
 
N

Neil

James,

You can use this function when and where you need it in your application. If
for example, you had a command button on your form (named cmdVerify in the
example) and a textbox where the credit card number is entered (named
txtCardNumber in the example) the code would look something like:

Private Sub cmdVerify_Click()

Dim bResult As Boolean
' Check credit card number
bResult = CheckCard(Me.txtCardNumber)
' Check the result
If bResult Then ' This is the same as saying If bResult = True
' Other statements go here
Else
' Display an error message
MsgBox "Incorrect Credit Card Number has been entered. Please
Retry.", vbOKOnly + vbExclamation, "Incorrect Number"
End If

End Sub

In the above example, the text entered into the text field (txtCardNumber)
is passed into the function CheckCard as a String. The argument that is
passed in can be anything, but the function (CheckCard) will only return
True if the credit card number is valid (this value is stored in our Boolean
variable named bResult). The variable bResult is then checked and if False
was returned from the CheckCard function, an error message would be
displayed to the user.

Note that this procedure will only check if the number entered 'looks like'
a credit card number. Not if the credit card itself actually exists.

Hope this helps,

Neil.
 
N

Neil

As far as a am aware, you cant assign the focus to the control that has just
lost it (hope that makes sense) which is what I assume you are trying to do.
A workaround for this would be to use the BeforeUpdate event of the Credit
Card text box. This has a cancel argument which you can set to true if you
dont want the textbox to be updated (in this case, if someone enteres an
incorrect card number). The procedure would look like:

Private Sub txtCreditCardNumber_BeforeUpdate(Cancel As Integer)

Dim bResult As Boolean
' Check for blank string
If Me.txtCreditCardNumber = "" Or IsNull(Me.txtCreditCardNumber) Then
' Exit the sub - no need to verify text input
Exit Sub
End If
' Check credit card number
bResult = CheckCard(Me.txtCreditCardNumber)
' Check the result
If bResult Then ' This is the same as saying If bResult = True
' Other statements go here
Else
' Display an error message
MsgBox "Incorrect Credit Card Number has been entered. Please Retry.",
vbOKOnly + vbExclamation, "Incorrect Number"
' Cancel the update event
Cancel = true
End If

End Sub

The focus will now remain on txtCreditCardNumber until a correct number is
entered (or if no number has been entered).

HTH,

Neil.
 
J

James

Thanks that worked.


Neil said:
As far as a am aware, you cant assign the focus to the control that has just
lost it (hope that makes sense) which is what I assume you are trying to do.
A workaround for this would be to use the BeforeUpdate event of the Credit
Card text box. This has a cancel argument which you can set to true if you
dont want the textbox to be updated (in this case, if someone enteres an
incorrect card number). The procedure would look like:

Private Sub txtCreditCardNumber_BeforeUpdate(Cancel As Integer)

Dim bResult As Boolean
' Check for blank string
If Me.txtCreditCardNumber = "" Or IsNull(Me.txtCreditCardNumber) Then
' Exit the sub - no need to verify text input
Exit Sub
End If
' Check credit card number
bResult = CheckCard(Me.txtCreditCardNumber)
' Check the result
If bResult Then ' This is the same as saying If bResult = True
' Other statements go here
Else
' Display an error message
MsgBox "Incorrect Credit Card Number has been entered. Please Retry.",
vbOKOnly + vbExclamation, "Incorrect Number"
' Cancel the update event
Cancel = true
End If

End Sub

The focus will now remain on txtCreditCardNumber until a correct number is
entered (or if no number has been entered).

HTH,

Neil.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top