Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does this script do?

Status
Not open for further replies.

WannaLearn

Programmer
Jul 10, 2001
210
0
0
US
Code:
function cccheck (formobj, type, no)
{

	for(i = 0; i < formobj.elements[type].length; i++)
	{
		var cctype=&quot;&quot;;
		if(formobj.elements[type][i].checked)
		{
			cctype = formobj.elements[type][i].value;
			break;
		}
	}

	if(cctype==&quot;&quot;)
	{
		alert (&quot;Please select a credit card!&quot;);
		return false;
	}

	var ccno=formobj.elements[no].value;

	//check number of digits
	if(
	  !(
		  (cctype == 'Mastercard' && ccno.length == 16)	||
		  (cctype == 'Visa' && (ccno.length == 13 || ccno.length == 16)) ||
		  (cctype == 'Amex' && ccno.length == 15) ||
		  (cctype == 'Discover' && ccno.length == 16)
	   )
	  )
	{
		alert (&quot;Invalid credit card number length!&quot;);
		return false;
	}
	var sum = 0;
  for (i = 0; i < ccno.length; i++)
	{
		var intval = parseInt(ccno.charAt(ccno.length-i-1));
		if(isNaN(intval))
		{
			alert (&quot;Invalid credit card number!&quot;);
			return false;
		}
		if ((i%2) != 0) intval = intval*2;
		sum = sum + intval%10;
		if (intval > 9) sum = sum + 1;
	}
	var modno = sum%10;
	if (modno != 0)
	{
		alert (&quot;Invalid credit card number!&quot;);
		return false;
	}
	else
		return true;
}

I was just wondering. We use that here and I just wanted to know. Thanks a lot.
 
In case you just want to know what this script does in general... It checks if a user has entered a valid credit card number. It makes a distinction between the different cc types (Amex,Visa etc.) and then checks if the provided number has the appropriate length.

If the length is correct it checks if the cc# is theoretically existent.

Hope this helps
 
Hi, I know this is a credit card check script, but I was wondering what this portion does in detail. The first part I can understand, the script checks if the cc value is correct, but when I test the script and enter the appropriate length (like Mastercard with 16 digits, all numeric) I get the alert saying: Invalid credit card number!
Why? If the length is correct and the cc type is correct why do I get that alert? I typed in random numbers, like 4125669310254560.
Thank you. :)


Code:
if ((i%2) != 0) intval = intval*2;
                                 sum = sum + intval%10;
                                 if (intval > 9) sum = sum + 1;
                             }
                             var modno = sum%10;
                             if (modno != 0)
                             {
                                 alert (&quot;Invalid credit card number!&quot;);
                                 return false;
                             }
 
Hey wannaLearn,
You are right, it will always alert saying &quot;Invalid Credit Card Number&quot; if you pick any random number. Somehow you got access to the logic that is used by Credit card agencies to check if credit card number is valid or not. Credit card has always a regular pattern that is chosen for it's number. If you provide the correct Credit card number it will not give any alert. I used the above mentioned javaScript function and it worked like charm for correct credit card number. By the way where did you get this code [bigglasses]
Hope it will help. If you want to closely observe it then use debugger and follow the code logic minutely. You will be able to crack down the pattern for valid credit card number [pipe]
 
Hi Raxg, thanks for getting back to me. So you're telling me that this script will work perfectly with a VALID CC number, but a 'made-up' number it'll throw that error?
But how does the script know what number is valid and what number isn't? I mean, does it check against a db with that cc company?
You said the script uses a formula that the CC companies use to handout the CC numbers, is that this formula:

if ((i%2) != 0) intval = intval*2;
sum = sum + intval%10;
if (intval > 9) sum = sum + 1;
}
var modno = sum%10;
if (modno != 0)


And lastly, can you exmplain what this script does line by line? Mainly the portion in red?




PS: I got this script from my job, we have people paying tickets and etc. online. I came across this script while doing a code check, and wondered why the CC # I entered was wrong. Thanks.
 
Hi Wannalearn,
Here is the answer for your query. Hope it might help you.
I am enclosing here two sample cases one with correct credit card number and another with your random number that you provided. Hope sample correct credit card number will not be misused ;-).In each cases you can figure out the final sum are 60 (correct) and 66 (random number) respectively. 60 is exact divisible of 10 with ramainder 0 where 66 leaves ramiander 6 that is non zero. (ref code
var modno = sum%10; if (modno != 0) alert(--) )

ccno.length-i-1 = P
parseInt(P) = Q

card number = 4021995040005926
--------------------------------------------
I | P| Q |intval|(i%2)| Sum|
--- |---|-----|------|------|-----|
0 15 6 6 0 6
1 14 2 4 1 10
2 13 9 9 0 19
3 12 5 10 1 20
4 11 0 0 0 20
5 10 0 0 1 20
6 9 0 0 0 20
7 8 4 8 1 28
8 7 0 0 0 28
9 6 5 10 1 29
10 5 9 9 0 38
11 4 9 18 1 47
12 3 1 1 0 48
13 2 2 4 1 52
14 1 0 0 0 52
15 0 4 8 1 60


card number = 4125669310254560

------------------------------------------

0 15 0 0 0 0
1 14 6 12 1 3
2 13 5 5 0 8
3 12 4 8 1 16
4 11 5 5 0 21
5 10 2 4 1 25
6 9 0 0 0 25
7 8 1 2 1 27
8 7 3 3 0 30
9 6 9 18 1 39
10 5 6 6 0 45
11 4 6 12 1 48
12 3 5 5 0 53
13 2 2 4 1 57
14 1 1 1 0 58
15 0 4 8 1 66


Hope this will help [pipe]

Experience teaches slowly and at the cost of mistakes :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top