Function to valid CC not working

# Thread: Function to valid CC not working

1. Senior Member
Join Date
Dec 1969
Posts
532

## Function to valid CC not working

Hi all,<BR>This one is stumping me real bad.<BR><BR>Basically, I need to make sure that customers select the right credit card type (paymentm);<BR>I want to make sure that each card begins with the correct prefix.<BR><BR>For instance, Visa begins with 4, discover begins with 6011, etc;<BR><BR>I want to make sure that each card has the correct lenght, and I want to make sure that a customer would not attempt to use an invalid card.<BR><BR>So far, each time I test the code, it always says:<BR><BR>Warning: invalid Number Length.<BR><BR>Can anyone please tell me what I am doing wrong?<BR><BR>Please excuse the long code:<BR><BR>When I tested isValidCard() function, it gets values that I entered passed to it.<BR><BR>But when I tested isValidLength() function, Credit card type (ct) is getting values passed but cl (credit card length ) is not.<BR><BR>Can anyone,please help..<BR><BR>This is driving me bananas.<BR><BR>Thanks in advance<BR><BR>Function getNumbersOnly(cn)<BR>Dim sb<BR>Dim i<BR><BR> For i = 1 To Len(cn)<BR> If Asc(Mid(cn, i, 1)) &#062;= 47 And Asc(Mid(cn, i, 1)) &#060;= 57 Then<BR> sb = sb & Mid(cn, i, 1)<BR> End If<BR> Next<BR><BR> getNumbersOnly = sb<BR><BR>End Function<BR><BR>Function isValidCard(ct, cn)<BR>&#039;Response.write "isValidLength() ct = " & ct & " and cn = "& cn<BR>&#039;response.end<BR><BR>Dim sb<BR>Dim cardno(18)<BR>Dim i<BR><BR> sb = getNumbersOnly(cn)<BR><BR> For i = 1 To Len(sb)<BR> cardno(i) = CInt(Mid(sb, i, 1))<BR> Next<BR><BR> If isValidLength(ct, Len(sb)) Then<BR> If isValidPrefix(ct, sb) Then<BR> If isValidCheckDigit(ct, sb) Then<BR> isValidCard = 0<BR> Else<BR><BR> isValidCard = 3<BR> End If<BR> Else<BR><BR> isValidCard = 2<BR> End If<BR> Else<BR><BR> isValidCard = 1<BR> End If<BR><BR>End Function<BR><BR>Function isValidLength(ct, cl)<BR>&#039;Response.write "isValidLength() ct = " & ct & " and c1 = "& c1<BR>&#039;response.end<BR> If ct = "enRoute" Then<BR> If cl = 15 Then<BR> isValidLength = True<BR> Exit Function<BR> End If<BR><BR> ElseIf ct = "DINERSCLUB" Then<BR> If cl = 14 Then<BR> isValidLength = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "AMERICANEXPRESS" Then<BR> If cl = 15 Then<BR> isValidLength = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "VISA" Then<BR> If cl = 13 Or cl = 16 Then<BR> isValidLength = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "MASTERCARD" Then<BR> If cl = 16 Then<BR> isValidLength = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "DISCOVER" Then<BR> If cl = 16 Then<BR> isValidLength = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "JCB" Then<BR> If cl = 15 Or cl = 16 Then<BR> isValidLength = True<BR> Exit Function<BR> End If<BR> End If<BR><BR> isValidLength = False<BR><BR>End Function<BR><BR>Function isValidPrefix(ct, cn)<BR><BR> If ct = "enRoute" Then<BR> If "2014" = Mid(cn, 1, 4) Or "2149" = Mid(cn, 1, 4) Then<BR> isValidPrefix = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "DINERSCLUB" Then<BR> If Mid(cn, 1, 2) = "36" Or Mid(cn, 1, 2) = "38" Or (CInt(Mid(cn, 1, 3)) &#062;= 300 And CInt(Mid(cn, 1, 3)) &#062;= 305) Then<BR> isValidPrefix = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "AMERICANEXPRESS" Then<BR> If "34" = Mid(cn, 1, 2) Or "37" = Mid(cn, 1, 2) Then<BR> isValidPrefix = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "VISA" Then<BR> If "4" = Mid(cn, 1, 1) Then<BR> isValidPrefix = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "MASTERCARD" Then<BR> If CInt(Mid(cn, 1, 2)) &#062;= 51 And CInt(Mid(cn, 1, 2)) &#060;= 55 Then<BR> isValidPrefix = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "DISCOVER" Then<BR> If "6011" = Mid(cn, 1, 4) Then<BR> isValidPrefix = True<BR> Exit Function<BR> End If<BR> ElseIf ct = "JCB" Then<BR> If "2131" = Mid(cn, 1, 4) Or "1800" = Mid(cn, 1, 4) Or "3" = Mid(cn, 1, 1) Then<BR> isValidPrefix = True<BR> Exit Function<BR> End If<BR> End If<BR><BR> isValidPrefix = False<BR><BR>End Function<BR><BR>Function isValidCheckDigit(ct, cn)<BR>Dim chkTotal<BR>Dim slen<BR>Dim chkDigit, compDigit<BR>Dim altNum<BR>Dim doubleDigit<BR>Dim otherNumber<BR>Dim i, j<BR><BR> slen = Len(cn)<BR> altNum = True<BR> otherNumber = slen - 1<BR><BR> chkDigit = CInt(Right(cn, 1))<BR><BR> For i = otherNumber To 1 Step -1<BR> If altNum Then<BR> doubleDigit = CInt(Mid(cn, i, 1)) * 2<BR><BR> For j = 1 To Len(CStr(doubleDigit))<BR> chkTotal = chkTotal + CInt(Mid(doubleDigit, j, 1))<BR> Next<BR> Else<BR> chkTotal = chkTotal + Mid(cn, i, 1)<BR> End If<BR><BR> altNum = altNum Xor True<BR> Next<BR><BR> compDigit = 10 - (chkTotal Mod 10)<BR> If compDigit = 10 Then<BR> compDigit = 0<BR> End If<BR><BR> If compDigit = chkDigit Then<BR> isValidCheckDigit = True<BR> Exit Function<BR> End If<BR><BR> isValidCheckDigit = False<BR><BR>End Function<BR><BR>Function Validate(paymentm, cardno)<BR> Dim resp, respond<BR><BR> resp = isValidCard(paymentm, cardno)<BR><BR> If resp = 0 Then<BR> Response.Redirect "checkout.asp?msg=" & Server.URLEncode("Credit Card Approved!")<BR> Else<BR> If resp = 1 Then &#039;invalid length<BR> Response.Redirect "checkout.asp?msg=" & Server.URLEncode("Credit Card Warning: Invalid Number Length")<BR> ElseIf resp = 2 Then &#039;invalid prefix<BR> Response.Redirect "checkout.asp?msg=" & Server.URLEncode("Credit Card Warning: Invalid Prefix")<BR> ElseIf resp = 3 Then &#039;invalid check digit<BR> Response.Redirect "checkout.asp?msg=" & Server.URLEncode("Credit Card Warning: Invalid Check Digit")<BR> Else<BR> Response.Redirect "checkout.asp?msg=" & Server.URLEncode("Credit Card Warning: Unknown Error")<BR> End If<BR><BR> End If<BR><BR>End Function<BR><BR> &#039; ************************************************** *********************<BR>%&#062;<BR>&#060;%<BR> Validate Request.Form("paymentm"), Request.Form("cardno")<BR>

2. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: Function to valid CC not working

I always use int() when comparing ints.<BR><BR>If int(cl) = 15 Then <BR><BR>I&#039;m assuming cl = card length<BR><BR>try that, otherwise I will take a closer look.<BR><BR>jc<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
532

## RE: Function to valid CC not working

JC,<BR>Thanks for the response.<BR><BR>Actually, most of it is working now.<BR><BR>The only problem seems to be the conditional under Validate()function which says:<BR><BR><BR>If resp = 0 Then<BR>Response.Redirect "checkout.asp?msg=" & Server.URLEncode("credit Card Approved!")<BR><BR><BR>What I am trying to say here is that when all validations are done and the card seems to meet all requirements of a valid credit card, process all data on this same page.<BR><BR>I really don&#039;t want the redirect anymore.<BR><BR>Thanks again<BR>

4. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: Function to valid CC not working

If I were you I would do all my validation in javascript. Once the page has been submitted you know all the data is correct. Then all you have to do is update the db and send them to the next page.<BR><BR>jc

5. Senior Member
Join Date
Dec 1969
Posts
532

## RE: Function to valid CC not working

I actually started to do the validation in Javascript but it isn&#039;t one of my strongest suites.<BR><BR>It was error&#039;ing out so bad that when I posted the code to ask for help, I was told to thumb javascript because it isn&#039;t safe.<BR><BR>Now that I tried this, it is giving me hell, also.<BR><BR>I mean, it is doing all the validations accurately.<BR><BR>I just need to stop it from redirecting to checkout.asp.<BR><BR>Checkout is where form variables are.<BR><BR>They get passed to this page for validation and processing.<BR><BR>It shouldn&#039;t be redirecting to checkout page again.<BR><BR>That was instruction I received but it isn&#039;t helping me now.

6. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: Ok

Yeah, the logic is not there.<BR><BR>First off doing the validation using vbs takes more work js is easier. Js has no trips back to the server.<BR><BR>Send me the credit card and checkout page and I will take a look for you. There is a better way to do this for you and the user.<BR><BR>jc

7. Senior Member
Join Date
Dec 1969
Posts
532

## RE: Sorrry Jon...

I had to leave.<BR><BR>I suppose that when you said send you credit card, you meant the javascript, yes?<BR><BR>If so, here is the code:<BR><BR>&#060;SCRIPT LANGUAGE=javascript&#062;<BR>&#060;!--<BR>// Client script validates form field entries for credit card<BR>function validate(theForm)<BR>{<BR>if(document.cform.IsHidd en.value=="false")<BR>{<BR>if (theForm.cardname.value == "" &#124&#124 theForm.cardname.value.length &#060; 2)<BR>{<BR>alert("Please fill in the name found on your credit card.");<BR>theForm.cardname.focus() ;<BR>return false;<BR>}<BR>if ((theForm.paymentm = "Visa" &#124&#124 theForm.paymentm = "Disc" &#124&#124 theForm.paymentm = "MC" ) && (theForm.cardno.value == "" &#124&#124 theForm.cardno.value == "0000-0000-0000-0000")<BR>{<BR>alert("Please fill in the card number in this format: 0000-0000-0000-0000.");<BR>theForm.cardno.focus();<BR>return false;<BR>}<BR>return true;<BR>}<BR>}<BR>function isValidCreditCard(type, ccnum) {<BR><BR>validLength = true;<BR>if (type == "Visa") {<BR>if (ccnum.length != 19){validLength = false} // I ued 19, 16 digits plus 3 dashes<BR>// Visa: length 16, prefix 4, dashes optional.<BR>var re = /^4d{3}-?d{4}-?d{4}-?d{4}\$/;<BR>} if (type == "MC") {<BR>if (ccnum.length != 19){validLength = false}<BR>// Mastercard: length 16, prefix 51-55, dashes optional.<BR>var re = /^5[1-5]d{2}-?d{4}-?d{4}-?d{4}\$/;<BR>} if (type == "Disc") {<BR>if (ccnum.length != 19){validLength = false}<BR>// Discover: length 16, prefix 6011, dashes optional.<BR>var re = /^6011-?d{4}-?d{4}-?d{4}\$/;<BR>} if (type == "AmEx") {<BR>if (ccnum.length != 18){validLength = false}<BR>// American Express: length 15, prefix 34 or 37.<BR>var re = /^3[4,7]d{13}\$/;<BR>} if (type == "Diners") {<BR>if (ccnum.length != 17){validLength = false}<BR>// Diners: length 14, prefix 30, 36, or 38.<BR>var re = /^3[0,6,8]d{12}\$/;<BR>}<BR>if (!validLength) return false;<BR>if (!re.test(ccnum)) return false;<BR>// Checksum ("Mod 10")<BR>// Add even digits in even length strings or odd digits in odd length strings.<BR>var checksum = 0;<BR>for (var i=(2-(ccnum.length % 2)); i&#060;=ccnum.length; i+=2) {<BR>checksum += parseInt(ccnum.charAt(i-1));<BR>}<BR>// Analyze odd digits in even length strings or even digits in odd length strings.<BR>for (var i=(ccnum.length % 2) + 1; i&#060;ccnum.length; i+=2) {<BR>var digit = parseInt(ccnum.charAt(i-1)) * 2;<BR>if (digit &#060; 10) { checksum += digit; } else { checksum += (digit-9); }<BR>}<BR>if ((checksum % 10) == 0) return true; else return false;<BR>}<BR>&#060;/script&#062;<BR><BR>****************************** ***<BR>Invoking the script with this<BR>*********************************<BR>&#060 ;td width="350"&#062; <BR> &#060;select name="paymentm" onChange="isValidCreditCard(this)"&#062; <BR> &#060;option value="Disc"&#062;Discover&#060;/option&#062; <BR> &#060;option selected value="Visa"&#062;Visa&#060;/option&#062; <BR> &#060;option value="AmEx"&#062;American Express&#060;/option&#062; <BR> &#060;option value="MC"&#062;Mastercard&#060;/option&#062; <BR> &#060;option value="Diners"&#062;Diner&#039;s Club&#060;/option&#062; <BR> &#060;/select&#062; <BR> &#060;/td&#062;<BR><BR>All of these are in checkout.asp file.<BR><BR>&#060;form name="cform" method="post" action="process.asp"&#062;<BR> &#060;table width="500" cellspacing="2" cellpadding="4" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td colspan="2"&#062;<BR> &#060;font color="000000" face="" &#062;<BR> &#060;b&#062;Payment information&#060;/b&#062;<BR> &#060;/font&#062;<BR> &#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr&#062;<BR> &#060;td width="130"&#062;Payment:&#060;/td&#062;<BR> &#060;td width="480"&#062;<BR> &#060;select name="paymentm" onChange="isValidCreditCard(this)"&#062;<BR> &#060;option value="Disc"&#062;Discover&#060;/option&#062;<BR> &#060;option selected value="Visa"&#062;Visa&#060;/option&#062;<BR> &#060;option value="AmEx"&#062;American Express&#060;/option&#062;<BR> &#060;option value="MC"&#062;MasterCard&#060;/option&#062;<BR> &#060;option value="Diners"&#062;Diner&#039;s Club&#060;/option&#062;<BR> &#060;option value="JCB"&#062;JCB&#060;/option&#062;<BR> &#060;option value="enRoute"&#062;enRoute&#060;/option&#062;<BR> &#060;/select&#062;<BR> &#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;/table&#062;<BR> <BR><BR> &#060;table width="500" cellspacing="2" cellpadding="4" border="1" ID="Show_1" style="display: "&#062;<BR> &#060;tr&#062;<BR> &#060;td colspan="2" class="body1"&#062;<BR> &#060;FONT color="000000"&#062;<BR> &#060;STRONG&#062;Card information&#060;/STRONG&#062;&#060;/FONT&#062;<BR> &#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr&#062;<BR> &#060;tr&#062;<BR> &#060;td width="130" class="body1"&#062;Card name:&#060;/td&#062;<BR> &#060;td width="350"&#062;&#060;input type="text" name="cardname" value="&#060;%= Session("cardname")%&#062;"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr&#062;<BR> &#060;td class="body1"&#062;Card no.:&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="cardno" maxlength="16" value="&#060;%= Session("cardno")%&#062;"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr&#062;<BR> &#060;td class="body1"&#062;Expiration date:&#060;/td&#062;<BR> &#060;td&#062;<BR> &#060;select name="expMonth"&#062;<BR> &#060;%sMonth = session("expMonth")<BR> If sMonth &#060;&#062; "" then<BR> Response.Write "&#060;option value=" & sMonth & "&#062;" & sMonth & "&#060;/option&#062;"<BR> End If%&#062;<BR> &#060;option value="1"&#062;1&#060;/option&#062;<BR> &#060;option value="2"&#062;2&#060;/option&#062;<BR> &#060;option value="3"&#062;3&#060;/option&#062;<BR> &#060;option value="4"&#062;4&#060;/option&#062;<BR> &#060;option value="5"&#062;5&#060;/option&#062;<BR> &#060;option value="6"&#062;6&#060;/option&#062;<BR> &#060;option value="6"&#062;6&#060;/option&#062;<BR> &#060;option value="7"&#062;7&#060;/option&#062;<BR> &#060;option value="8"&#062;8&#060;/option&#062;<BR> &#060;option value="9"&#062;9&#060;/option&#062;<BR> &#060;option value="10"&#062;10&#060;/option&#062;<BR> &#060;option value="11"&#062;11&#060;/option&#062;<BR> &#060;option value="12"&#062;12&#060;/option&#062;<BR> &#060;/select&#062;<BR> &#060;select name="expYear"&#062;<BR> &#060;%sYear = session("expYear")<BR> If sYear &#060;&#062; "" then<BR> Response.Write "&#060;option value=" & sYear & "&#062;" & sYear & "&#060;/option&#062;"<BR> End If%&#062;<BR> &#060;option value="05"&#062;05&#060;/option&#062;<BR> &#060;option value="06"&#062;06&#060;/option&#062;<BR> &#060;option value="07"&#062;07&#060;/option&#062;<BR> &#060;option value="08"&#062;08&#060;/option&#062;<BR> &#060;option value="09"&#062;09&#060;/option&#062;<BR> &#060;option value="10"&#062;10&#060;/option&#062;<BR> &#060;option value="11"&#062;11&#060;/option&#062;<BR> &#060;/select&#062;<BR> &#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr&#062;<BR> &#060;td&#062;<BR> Card address<BR><BR> (if different from<BR><BR> your address):<BR> &#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="cardaddress" value="&#060;%= Session("cardaddress")%&#062;"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR><BR><BR> &#060;/TABLE&#062;<BR><BR> &#060;INPUT type="hidden" name="Ototal" value="&#060;%= oTotal%&#062;"&#062;<BR> &#060;P&#062;&#060;INPUT name="action" type=submit value="Place Order now!"&#062;<BR> &#060;/FORM&#062;<BR> &#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>Once again, JC, I apologize

8. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: Sorrry Jon...

If you still want help email me at:<BR><BR>clhosting [at] hotmail.com<BR><BR>I wont start until I hear from you.<BR><BR>jc

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•