/    Sign up×
Community /Pin to ProfileBookmark

Credit Card Validation. Whats wrong?

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

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 10.2003 — This line: [i]if(str == ""[/i] should look like this: [i]if(str == "")[/i]
Copy linkTweet thisAlerts:
@cols2910authorApr 10.2003 — 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
Copy linkTweet thisAlerts:
@JonaApr 10.2003 — Actually, I rarely use a submit button. Instead, I use Javascript to submit the form after validation takes place. See the example [url=http://c-mm.tripod.com/qwerty/signup.html]here[/url].
Copy linkTweet thisAlerts:
@cols2910authorApr 10.2003 — 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.
Copy linkTweet thisAlerts:
@JonaApr 10.2003 — Hmm... Maybe you can just tell me which parts of the code you don't understand [i]or[/i] tell me which parts are causing errors. I'm sorry, but I'm not going to run through all that code for you.. ?
Copy linkTweet thisAlerts:
@NedalsApr 10.2003 — [i]Do you think the Submit button is the problem?[/i]

No. It's fine.

I see two possible problems...

default:

prefixRegExp = /^$/;

alert("Card type not found");

[b]return false; break; [/b]

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
Copy linkTweet thisAlerts:
@cols2910authorApr 11.2003 — Thanks Jona

I will try your suggestions.

Many thanks in the meantime for all your help.

Regards

Colin
Copy linkTweet thisAlerts:
@cols2910authorApr 11.2003 — Hi Nedals

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

Many thanks for taking the time to help.

Regards

Colin
Copy linkTweet thisAlerts:
@cols2910authorApr 11.2003 — 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>
Copy linkTweet thisAlerts:
@NedalsApr 11.2003 — 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>
Copy linkTweet thisAlerts:
@cols2910authorApr 11.2003 — 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
Copy linkTweet thisAlerts:
@cols2910authorApr 11.2003 — 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
Copy linkTweet thisAlerts:
@JonaApr 11.2003 — 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.
×

Success!

Help @cols2910 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...