Click to See Complete Forum and Search --> : Credit Card Validation. Whats wrong?


cols2910
04-10-2003, 03:30 PM
Hi all

I am trying the code below to validate credit catd numbers. I hasten to add the code is not mine, but I pretty much understand whats going on. However, I am having great difficulty in getting it to work, as even if I enter nothing, it still looks to send to my cgi script. Anyone any ideas as to why this isn't working?

Many thanks in advance. Heres the code

<code>

<html>
<head>

<title>Credit card entry and validation</title>

<Link rel = "stylesheet" Type="text/css" Href="Styles.css">

<SCRIPT LANGUAGE = JAVASCRIPT>

function Validate()
{
// get to field
var obj = document.getElementById("cardNumber");

// Value of field
var str = obj.value;

if(str == ""
{
alert("You must enter a valid credit card number");
obj.focus();
return false;
}

var isValid = false;
var ccCheckRegExp = /[^\d ]/;
isValid = !ccCheckRegExp.test(str);

if (isValid)
{
var strOnly = str.replace(/ /g,"");
var strLength = strOnly.length;
var lengthIsValid = false;
var prefixIsValid = false;
var prefixRegExp;

switch(cardType)
{
case "mastercard":
lengthIsValid = (strLength == 16);
prefixRegExp = /^5[1-5]/;
break;

case "visa":
lengthIsValid = (strLength == 16 || strLength == 13);
prefixRegExp = /^4/;
break;

case "amex":
lengthIsValid = (strLength == 15);
prefixRegExp = /^3(4|7)/;
break;

default:
prefixRegExp = /^$/;
alert("Card type not found");
}

prefixIsValid = prefixRegExp.test(strsOnly);
isValid = prefixIsValid && lengthIsValid;
}

if(isValid)
{
var numberProduct;
var numberProductDigitIndex;
var checkSumTotal = 0;

for (digitCounter = strLength - 1;
digitCounter >= 0;
digitCounter--)
{
checkSumTotal += parseInt (strsOnly.charAt(digitCounter));
digitCounter--;
numberProduct = String((strsOnly.charAt(digitCounter) * 2));
for (var productDigitCounter = 0;
productDigitCounter < numberProduct.length;
productDigitCounter++)
{
checkSumTotal +=
parseInt(numberProduct.charAt(productDigitCounter));
}
}

isValid = (checkSumTotal % 10 == 0);
}

return isValid;

if(isValid)
{
return true;
alert("True");

}

else

{
return false;
alert("False");


}
}




</SCRIPT>

</head>

<body bgcolor="#000000" text="#ffffff">

<form onSubmit="return Validate()" Action="/cgi-bin/Project/CC.cgi" Method="POst">
<table border="1" align="center" cellpadding="3">
<tr bgcolor="blue">
<td CLASS="Second">Card Type</td>
<td CLASS="Second"><select name="cardType">
<option value="visa" selected>Visa
<option value="mastercard">Mastercard
<option value="amex">Amex
</option></select></td></tr>
<tr bgcolor="blue"><td CLASS="Second">Credit Card Number:</td><td><input name="str" size="19" maxlength="19"></td></tr>
<tr bgcolor="blue"><td CLASS="Second">Expiration Date (MM/YY):</td><td><input name="expDate" size="7"></td>

</tr>

<tr bgcolor="Blue">
<td colspan="2" align="center">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>

</html>

</code>

Regards
Colin

Jona
04-10-2003, 04:23 PM
This line: if(str == "" should look like this: if(str == "")

cols2910
04-10-2003, 04:41 PM
Hey Jona

Thanks for taking the time to reply.

I amended the bracket (I must have overlooked that last time. Getting square eyes with this), but still no validation takes place. If you press the submit button, it goes straight for the CGI script, even if the fields are blank.

I want to validate the CC number on the client side.

Cheers
Colin

Jona
04-10-2003, 04:56 PM
Actually, I rarely use a submit button. Instead, I use Javascript to submit the form after validation takes place. See the example here (http://c-mm.tripod.com/qwerty/signup.html).

cols2910
04-10-2003, 05:09 PM
Do you think the Submit button is the problem? I have another script that has one that is layed out in a similar fashion, and it works well. But this one.....aaaah I'm lost with this one mate.

Jona
04-10-2003, 05:26 PM
Hmm... Maybe you can just tell me which parts of the code you don't understand or tell me which parts are causing errors. I'm sorry, but I'm not going to run through all that code for you.. :)

Nedals
04-10-2003, 06:21 PM
Do you think the Submit button is the problem?
No. It's fine.

I see two possible problems...

default:
prefixRegExp = /^$/;
alert("Card type not found");
return false; break;

I did not check your logic though carefully but it looks like 'isValid' may not be correctly set. Also you use this line
'return isValid' toward the end of your script. Anything after that will be ignored.
Put an alert(isValid); prior to that line to test its value

cols2910
04-11-2003, 02:22 AM
Thanks Jona

I will try your suggestions.

Many thanks in the meantime for all your help.

Regards
Colin

cols2910
04-11-2003, 02:24 AM
Hi Nedals

Thanks for your suggestions. I will give those a try also.

Many thanks for taking the time to help.

Regards
Colin

cols2910
04-11-2003, 02:53 AM
Okay guys, still no joy, as it would appear no validation is being done at all. I was wondering if I was calling the function correctly, however, I attach the original code. Note there is not HTML form attached to it, and I feel that may be my problem, or that I have made a dramatic error in altering the code slightly.

Anyway here is the original. Let me know what you think.

Bets regards
Colin

<code>

Validate.prototype.isValidCreditCardNumber = function(cardNumber, cardType)
{
var isValid = false;
var ccCheckRegExp = /[^\d ]/;
isValid = !ccCheckRegExp.test(cardNumber);

if (isValid)
{
var cardNumbersOnly = cardNumber.replace(/ /g,"");
var cardNumberLength = cardNumbersOnly.length;
var lengthIsValid = false;
var prefixIsValid = false;
var prefixRegExp;

switch(cardType)
{
case "mastercard":
lengthIsValid = (cardNumberLength == 16);
prefixRegExp = /^5[1-5]/;
break;

case "visa":
lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
prefixRegExp = /^4/;
break;

case "amex":
lengthIsValid = (cardNumberLength == 15);
prefixRegExp = /^3(4|7)/;
break;

default:
prefixRegExp = /^$/;
alert("Card type not found");
}

prefixIsValid = prefixRegExp.test(cardNumbersOnly);
isValid = prefixIsValid && lengthIsValid;
}

if (isValid)
{
var numberProduct;
var numberProductDigitIndex;
var checkSumTotal = 0;

for (digitCounter = cardNumberLength - 1;
digitCounter >= 0;
digitCounter--)
{
checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
digitCounter--;
numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
for (var productDigitCounter = 0;
productDigitCounter < numberProduct.length;
productDigitCounter++)
{
checkSumTotal +=
parseInt(numberProduct.charAt(productDigitCounter));
}
}

isValid = (checkSumTotal % 10 == 0);
}

return isValid;
}

</code>

Nedals
04-11-2003, 12:44 PM
Rather than go thought your script, you might want to try this instead.

<html><head><title>Untitled</title>
<script type="text/javascript">
<!--
function ccValidate(ccnumber) {
cc = ccnumber.replace(/[^0-9]/g,''); // remove any spaces or non-numeric values
var len = cc.length; checksum = 0;
if (len==13 || len==15 || len==16 ) {
while (len > 0) { // mod 10 checksum
checksum += eval(cc.charAt(len-1)); len--;
digit = eval(cc.charAt(len-1)); len--;
checksum += (digit < 9) ? (digit * 2) % 9 : 9;
}
if (checksum%10 == 0) { return true }
}
alert('invalid card'); return false;
}
//-->
</script>
</head>

<body bgcolor="#ffffff">
<form action="#" onSubmit="return ccValidate(this.cc.value)">
<input type="text" name="cc"><br>
<input type="submit">
</form>

</body>
</html>

cols2910
04-11-2003, 06:53 PM
Nedals

You the man.

Thanks very much. Thats a lot more straightforward than what I was trying to do. Exactly what I was looking for.

Thanks again.

Regards
Colin

cols2910
04-11-2003, 06:56 PM
Jona

Thanks for all your help man. I can't blame you for not wishing to run through all that code. It got a bit baffling I must admit.

Anyway, I appreciate your help all the same.

All the best.

Kind regards
Colin

Jona
04-11-2003, 06:59 PM
Well, if I knew you were looking for an alternative rather than figuring out how to make the original code work I wouldn've helped more.