0

Could anyone help me with a script I've written. As part of an assignment I have to write checksums to validate various numbers (VISA,Euro Bank Notes) and if I got this going I could modify to validate the other requirements but can't see what I've done wrong.....I should add that this is my first time writing javascript!

I'm trying to use the first function to ensure that only numbers are entered but the script only runs as far as the loop.

As the script doesn't get passed the first loop I don't know if the 2nd function works.

function CCValidate()
{
	var serial;		
	var valid = true;
	serial =prompt("Please Enter A Credit Card Number For Validation", "4499228308017422");
	
	if (serial.lenght()==0)
	{
		valid=false;
	}
	if (serial.lenght !=16)
	{
		valid=false;
	}
	var x;										
		for (x=0;x<16;x++)
		{
		parseInt(serial[x]);
		if (x!>0 && x!<9)
		{
		valid=false;
		}
		}

}
return valid;

function CCChecksum()
{
	var i;
	var total=0;
	var total = total+a;
	for(i=0;i<16;i=i+2)
	{
	var a;						
	a=parseInt(serial[1]*2);	
		if (a>9)	
		{
			a = a+'';
			a = parseInt(a[0])+parseInt(a[1]);
		}

	}


		var j;
	parseInt(serial[15]);
	{
		j = serial[1]+serial[3]+serial[5]+serial[7]+serial[9]+serial[11]+serial[13]+serial[15];
	}

var result=0;
	result = total + j;
	
	if (result %10=0)
	{
		alert("The Credit Card Number You Have Entered Is Valid");
	}
	else alert("The Number Entered Is Not A Valid Credit Card Number");
}

Any help would be greatly appreciated.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by twiss
0

First of all, lenght should be length. Also, it is not a function, but a property. If it is 16, it can never be 0. !> should be < and !< should be >. So, the first function can be rewritten to:

var serial = prompt("Please Enter A Credit Card Number For Validation", "4499228308017422");
var valid = serial.length == 16;
var n;
for (var x = 0; x < 16 && valid; x++) if ((n = parseInt(serial[x])) < 0 || n > 9) valid = false;
return valid;

Edit: the second function also won't work, but I'm not sure what it's supposed to do?

Edited by twiss: n/a

0

An even better version would be:

var serial = prompt("Please Enter A Credit Card Number For Validation", "4499228308017422");
var valid = serial.length == 16;
for (var x = 0; x < 16 && valid; x++) valid = !isNaN(parseInt(serial[x]));
return valid;

Edited by twiss: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.