Solved

ASP Credit Card Validation

Posted on 1998-09-16
2
365 Views
Last Modified: 2013-12-20
How do you validate credit card info using ASP
I would like to check both number & expiry.
I do NOT require credit card processing, validation only.
0
Comment
Question by:hamishd
2 Comments
 
LVL 10

Accepted Solution

by:
MasseyM earned 100 total points
Comment Utility
Here is the HTML:


<html>
<head>
<title>Credit Card Number Verification</title>
<script LANGUAGE="vbscript">
'
'Create two variables with global scope
'
     Dim paymethod
     Dim TheCardType

'
'This is the main sub routine and is called by the button's onclick event handler
'
Sub VerifyCard
    dim strCleanNo
    dim verified
    dim currdate

 'check the date if bad then quit
 If Not GoodDate() Then
  Exit Sub
 End If

 'take all spaces etc out of the string
 strCleanNo = CleanString()

 'determine the type of card and send execution off in the right direction
 Select Case TheCardType
 Case "amex"
     verified = verifyAmex(strCleanNo)
 Case "visa"

     'this is a nested select
     Select Case Len(strCleanNo)
     Case 13
          verified = verifyV(strCleanNo)
     Case 16
          verified = verifyMC(strCleanNo)
     Case Else
          verified = False
     End Select

 Case "mastercard"
     verified = verifyMC(strCleanNo)
 Case "discovery"
     verified = verifyMC(strCleanNo)
 Case Else
     Alert  "Please select a Card Type"
     Exit Sub
 End Select

 'is it ok or not?
 If Not verified Then
     i = MsgBox("INVALID CARD NUMBER",16,"Cannot Submit Data")
 Else
     i = MsgBox("The Card No. is OK",64,"Submitting Data....")
 End If
End Sub

'
'This is the function that checks the date
'
Function GoodDate()
 GoodDate = True
 If Not IsDate(Document.Form1.CardExp.Value) Then
     i = MsgBox("Invalid Expiry Date",16,"Cannot Submit Data")
     GoodDate = False
 Else
     currdate = Month(Now()) & " " & Year(Now())
     If DateValue(Document.Form1.CardExp.Value) < DateValue(currdate) Then
          i = MsgBox("THIS CARD HAS EXPIRED",16,"Cannot Submit Data")
          GoodDate = False
     End If
 End If
End Function

'
'This function validates an amex card
'
Function VerifyAmex(ByVal CardNo)
    Dim amexarray(14)
    Dim tot
    tot=0

 If Len(CardNo) <> 15 Then
     VerifyAmex = False
     Exit Function
 End If

 For x = 2 to 10 step 2
     amexarray(x-1) = Mid(CardNo,x,1)
 Next
 
 For x = 12 to 15
     amexarray(x-1) = Mid(CardNo,x,1)
 Next

 For x = 1 to 11 Step 2
     y = Mid(CardNo,x,1) * 2
     If y >= 10 Then
         amexarray(x-1) =  (y Mod 10) + 1
     Else
         amexarray(x-1) = y
     End If
 Next
 
 For x = 0 to 14
     tot = tot + CInt(amexarray(x))
 Next

 If tot Mod 10 = 0 Then
     VerifyAmex = True
 Else
     VerifyAmex = False
 End If
 
End Function

'
'This function checks visa 13 digit cards
'
Function VerifyV(ByVal CardNo)
    Dim vArray(12)
    Dim tot
    tot=0

 For x = 2 to 12 step 2
     vArray(x-1) = Mid(CardNo,x,1)
 Next
     vArray(12) = Mid(CardNo,13,1)
 
 'Mod returns the remainder of a division
 For x = 1 to 11 Step 2
     y = Mid(CardNo,x,1) * 2
     If y >= 10 Then
         vArray(x-1) =  (y Mod 10) + 1
     Else
         vArray(x-1) = y
     End If
 Next
 
 For x = 0 to 12
     tot = tot + CInt(vArray(x))
 Next

 If tot Mod 10 = 0 Then
     VerifyV = True
 Else
     VerifyV = False
 End If
 
End Function

'
'Although it says MC it's actually a multi purpose 16 digit checker
'
Function VerifyMC(ByVal CardNo)
    Dim tot
    tot=0


 If Len(CardNo) <> 16 Then
     VerifyMC = False
     Exit Function
 End If

 For x = 2 to 16 step 2
     tot = tot + CInt(Mid(CardNo,x,1))
 Next

 For x = 1 to 15 Step 2
     y = Mid(CardNo,x,1) * 2
     If y >= 10 Then
         tot = tot + CInt((y Mod 10) + 1)
     Else
         tot = tot + CInt(y)
     End If
 Next
 
 If tot Mod 10 = 0 Then
     VerifyMC = True
 Else
     VerifyMC = False
 End If

End Function

'
'This function looks for numbers in the card number string, thereby
'removing all non numeric characters
'
Function CleanString()
    dim strLen
    dim strCounter
    dim strClean
    dim strDirty
 
 strDirty = Trim(Document.Form1.cardnumber.value)
 strLen = Len(strDirty)
 strClean = ""
 
 For strCounter = 1 to strLen
    If Asc(Mid(strDirty,strCounter,1)) < 58 AND Asc(Mid(strDirty,strCounter,1)) > 47 Then
        strClean = strClean  & Mid(strDirty,strCounter,1)
    End If
 Next

 CleanString = strClean
End Function

'et voila
</script>

</head>
<body BACKGROUND="bground.jpg">
<font FACE="arial" SIZE="2">
<center>
<table>
<td><img SRC="visa.gif" WIDTH="64" HEIGHT="43">
<td><img SRC="master.gif" WIDTH="72" HEIGHT="43">
<td><img SRC="amex.gif" WIDTH="43" HEIGHT="43">
</table>
<p>
<br><br>
<blockquote>
If you are paying by credit card, please complete this section
of the form.
</blockquote>
    <form NAME="Form1">

<table>
<td ALIGN="RIGHT"><b>Visa</b>
<td>
<input LANGUAGE="vbscript" TYPE="RADIO" VALUE="visa" ONCLICK="TheCardType= &quot;visa&quot;" NAME="cardtype">

<td ALIGN="RIGHT"><b>Mastercard</b>
<td><input LANGUAGE="vbscript" TYPE="RADIO" VALUE="mastercard" ONCLICK="TheCardType= &quot;mastercard&quot;" NAME="cardtype">
<tr>
<td ALIGN="RIGHT"><b>American Express</b>
<td><input LANGUAGE="vbscript" TYPE="RADIO" VALUE="amex" ONCLICK="TheCardType= &quot;amex&quot;" NAME="cardtype">

<td ALIGN="RIGHT"><b>Discover</b>
<td><input LANGUAGE="vbscript" TYPE="RADIO" VALUE="discovery" ONCLICK="TheCardType= &quot;discovery&quot;" NAME="cardtype">
<tr>
</table>
<p>
<table>
<td><b>Cardholder's Name</b>
<td><input TYPE="TEXT" SIZE="30" NAME="cardholder">
<tr>
<td><b>Card Number</b>
<td><input TYPE="TEXT" SIZE="20" NAME="cardnumber">
<tr>
<td><b>Card Expiration Date</b>
<td><input TYPE="TEXT" SIZE="5" NAME="cardexp">
<tr>
</table>
<p>
        <input LANGUAGE="vbscript" TYPE="button" VALUE="Check" ONCLICK=" call VerifyCard()">
        <input TYPE="RESET" VALUE="Clear/Reset">
    </form>
<br><br>

<font SIZE="1"><i>
This example appears courtesy of SAMS.NET<br>
from the book
<a HREF="../../books.htm">
<b>Laura Lemay's Web Workshop: ActiveX and VBScript</b>
</a>
<br>
by Paul Lomax
</i>
</font>

</body>
</html>
0
 

Author Comment

by:hamishd
Comment Utility
The credit card validation must occur within a CGI script. Do you have a CC validation in ASP?

There is a security issue validating CC's using javascript.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Read about why website design really matters in today's demanding market.
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now