Forum Moderators: open

Message Too Old, No Replies

ASP Credit Card Validator

Had to create my own - you can grab it here!

         

HyperGeek

9:15 pm on Jun 18, 2002 (gmt 0)

10+ Year Member



Got sick of looking for a Javascript solution to Ye Olde credit card validation process.

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>&nbsp;</FONT></TD>
<TD>
<INPUT TYPE="text" NAME="name"></TD>
</TR>
<TR>
<TD>
<FONT CLASS="reg">
<B>Card Type:</B>&nbsp;</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>&nbsp;</FONT></TD>
<TD>
<INPUT TYPE="text" NAME="number" MAXLENGTH="24"></TD>
</TR>
<TR>
<TD>
<FONT CLASS="reg">
<B>Expires On:</B>&nbsp;</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">&nbsp;</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>

txbakers

9:30 pm on Jun 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very nice. Take a few minutes and look at what you can do with ASP.NET - you'll be amazed at how easy validation is. You could probably do away with 2/3 of your code.

TallTroll

9:33 pm on Jun 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To clarify :

Is this script running the checksum to see whether the input could be a valid card number?

HyperGeek

10:01 pm on Jun 18, 2002 (gmt 0)

10+ Year Member



Basically this script is working off of this information:

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. :)

HyperGeek

10:13 pm on Jun 18, 2002 (gmt 0)

10+ Year Member



I've been keeping my eyes on ASP.NET.

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."

:)

Purple Martin

2:51 am on Jun 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Is this script running the checksum ... ?

Aren't the checksum equations closely guarded secrets? I would have thought that the credit card companies would be keeping the equations to themselves.

msr986

4:12 am on Jun 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are free perl routines on the web that you can grab for credit card checksums.

Not so secret!

Filipe

7:21 pm on Jun 19, 2002 (gmt 0)

10+ Year Member



I'm not sure ASP.NET can cut down on the code that much. I read about people writing about the wonder of labels, and things like that, but you can do the same kind of effect by not doing context-switching.

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.)

HyperGeek

7:25 pm on Jun 19, 2002 (gmt 0)

10+ Year Member



You can also use a loop to cut down on extraneous code.

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.

:)