Credit Card Validation. Whats wrong?

wxdqz

New Member
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=http://www.webdeveloper.com/forum/archive/index.php/"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
 
Back
Top