Solved

ASP Credit Card Validation

Posted on 1998-09-16
2
374 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
ID: 1855095
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
ID: 1855096
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to count occurrences of each item in an array.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

828 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