[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

ASP Credit Card Validation

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
hamishd
Asked:
hamishd
1 Solution
 
MasseyMCommented:
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
 
hamishdAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now