Forum Moderators: open
Here's a script I created to do exactly what's needed to check to see if a credit card RESEMBLES one that would be valid.
Mind you, this doesn't process the transaction - and you will have to do so manually (or by appending my script), but this is pretty solid and can use Javascript to validate the HTML form (so that no fields are left blank, numbers are entered where they need to be, etc.)
If any one decides to make improvements, I'd be interested in seeing the changes/modifications that you make. Especially if it involves adding more CC validation conditions.
It's nifty at best. :) But it's mine to share - so here you go...
<HTML>
<HEAD>
<TITLE>ASP Credit Card Validation</TITLE>
</HEAD>
<BODY>
<FORM METHOD="post" ACTION="/val.asp" NAME="cc" ONSUBMIT="return valform()">
<TABLE CELLPADDING="2" CELLSPACING="0" BORDER="0" WIDTH="0">
<TR>
<TD>
<FONT CLASS="reg">
<B>Cardholder's Name:</B> </FONT></TD>
<TD>
<INPUT TYPE="text" NAME="name"></TD>
</TR>
<TR>
<TD>
<FONT CLASS="reg">
<B>Card Type:</B> </FONT></TD>
<TD>
<SELECT NAME="cc_type">
<OPTION VALUE="1">Visa</OPTION>
<OPTION VALUE="2">MasterCard</OPTION>
<OPTION VALUE="3">American Express</OPTION>
<OPTION VALUE="4">Discover</OPTION>
</SELECT></TD>
</TR>
<TR>
<TD>
<FONT CLASS="reg">
<B>Card Number:</B> </FONT></TD>
<TD>
<INPUT TYPE="text" NAME="number" MAXLENGTH="24"></TD>
</TR>
<TR>
<TD>
<FONT CLASS="reg">
<B>Expires On:</B> </FONT></TD>
<TD>
<SELECT NAME="month">
<OPTION VALUE="1">01/January</OPTION><OPTION
VALUE="2">02/February</OPTION><OPTION VALUE="3">03/March</OPTION><OPTION
VALUE="4">04/April</OPTION><OPTION VALUE="5">05/May</OPTION><OPTION
VALUE="6">06/June</OPTION><OPTION VALUE="7">07/July</OPTION><OPTION
VALUE="8">08/August</OPTION><OPTION VALUE="9">09/September</OPTION><OPTION
VALUE="10">10/October</OPTION><OPTION VALUE="11">11/November</OPTION><OPTION
VALUE="12">12/December</OPTION></SELECT>, <INPUT TYPE="text" NAME="year"
SIZE="4" MAXLENGTH="4"> (ex: 2004)
</TD>
</TR>
<TR>
<TD><FONT CLASS="reg"> </FONT></TD>
<TD>
<INPUT TYPE="submit" VALUE="Go"></TD>
</TR>
</FORM>
</TABLE><P>
<B>
<%
DIM cc_number
cc_number = replace(request.Form("number")," ","")
'VISA Validation
IF request.Form("cc_Type") = "1" THEN
IF Len(cc_number) = "13" OR Len(cc_number) = "16" THEN
IF Left(cc_number,"1") = "4" THEN
response.Write "Valid"
ELSE
response.Write "Invalid"
END IF
ELSE
response.Write "Invalid"
END IF
'MASTERCARD Validation
ELSEIF request.Form("cc_Type") = "2" THEN
IF Len(cc_number) = "16" THEN
IF Left(cc_number,"1") = "5" THEN
response.Write "Valid"
ELSE
response.Write "Invalid"
END IF
ELSE
response.Write "Invalid"
END IF
'AMERICAN EXPRESS Validation
ELSEIF request.Form("cc_Type") = "3" THEN
IF Len(cc_number) = "15" THEN
IF Left(cc_number,"2") = "34" OR
Left(request.Form("number"),"2") = "37" THEN
response.Write "Valid"
ELSE
response.Write "Invalid"
END IF
ELSE
response.Write "Invalid"
END IF
'DISCOVER Validation
ELSEIF request.Form("cc_Type") = "4" THEN
IF Len(cc_number) = "16" THEN
IF Left(cc_number,"4") = "6011" THEN
response.Write "Valid"
ELSE
response.Write "Invalid"
END IF
ELSE
response.Write "Invalid"
END IF
END IF
%>
</B><P>
Input: <%=cc_number%><BR>
Length: <%=Len(cc_number)%> characters<BR>
<% IF request.Form("cc_Type") = "1" THEN %>
Prefix: <%=Left(cc_number,"1")%>
<% ELSEIF request.Form("cc_Type") = "2" THEN %>
Prefix: <%=Left(cc_number,"1")%>
<% ELSEIF request.Form("cc_Type") = "3" THEN %>
Prefix: <%=Left(cc_number,"2")%>
<% ELSEIF request.Form("cc_Type") = "4" THEN %>
Prefix: <%=Left(cc_number,"4")%>
<% END IF %>
</BODY>
</HTML>
VISA ---
Acceptable Prefix(s): 4
Acceptable Length: 13 or 16 characters
MC ---
Acceptable Prefix(s): 5
Acceptable Length: 16 characters
AMEX ---
Acceptable Prefix(s): 34 or 37
Acceptable Length: 15 characters
DISCOVER ---
Acceptable Prefix(s): 6011
Acceptable Length: 16 characters
Anything else is considered invalid. This is pretty much the process of "Simple Validation" that MIVA Merchant uses... but in ASP.
As of right now, I'm pretty sure that I've covered all bases to make sure that we get a valid CC# through to the processing dept.
Of course, everything can be tweaked, and should to accomodate the site you're using it for. This code is raw and took me approx. 10 minutes to write - so have at it. :)
I'm fairly new to ASP as it is and don't want to bite off more than I can chew. I'm self taught, so I like to work at a slow pace and really "get down" what I'm doing at that point in time.
I figure that I'll progress onto ASP.NET come October or so once I feel that I have completely grasped all that I need to know about the SQL/VBScript stuff I'm currently working with.
There was a point at which someone would say "You should have this variable defined like this..." and I would reply with, "Variable? OH! That DIM thing... okaaaay."
:)
Simply do all your processing code above, store all variable output into a single variable, and then just output that variable. It works the same way as a Label.text without the other properties (Label.length, etc.)
But as I said, it was a 10 minute jobber - and really not too bad for a functional validation script written in that small amount of time.
You also have to ask yourself...
Q: What difference in loading time would we really see if this script were smaller?
A: A fraction of a second I suppose.
:)