2 Replies - 592 Views - Last Post: 03 September 2012 - 03:58 AM

#1 07ozo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-September 12

anyone please help me

Posted 03 September 2012 - 03:23 AM

Create a page which allows a user to enter a credit card number and expiry date into a text field. When the user clicks a button, the page should check if the credit card number and expiry date is valid. A valid credit card number has 16 digits. A expiry date is valid if it has a 2 digit month followed by a 2 digit year. The month must be between 01 and 12 and the month and year must be greater than now.

Hint:
var d = new Date();
alert(d.getFullYear()); //displays 2012
alert(d.getMonth()); //displays the month number where january is 0 and december is 11


<HEAD>

<script LANGUAGE="Javascript">

var Cards = new makeArray(8);
Cards[0] = new CardType;
var MasterCard = Cards[0]= new CardType();

function CheckCardNumber(form) {
var tmpyear;
if (form.CardNumber.value.length == 0) {
alert("Please enter a Card Number.");
form.CardNumber.focus();
return;
}
if (form.ExpYear.value.length == 0) {
alert("Please enter the Expiration Year.");
form.ExpYear.focus();
return;
}
if (form.ExpYear.value > 96)
tmpyear = "19" + form.ExpYear.value;
else if (form.ExpYear.value < 12)
tmpyear = "20" + form.ExpYear.value;
else {
alert("The Expiration Year is not valid.");
return;
}
tmpmonth = form.ExpMon.options[form.ExpMon.selectedIndex].value;

if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
alert("This card has already expired.");
return;
}
card = form.CardType.options[form.CardType.selectedIndex].value;
var retval = eval(card + ".checkCardNumber(\"" + form.CardNumber.value +
"\", " + tmpyear + ", " + tmpmonth + ");");
cardname = "";
if (retval)

alert("This card number appears to be valid.");


else {
for (var n = 0; n < Cards.size; n++) {
if (Cards[n].checkCardNumber(form.CardNumber.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getCardType();
break;
   }

else {
alert("This card number is not valid.");
      }
   }
}

function checkCardNumber() {
var argv = checkCardNumber.arguments;
var argc = checkCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setCardNumber(cardnumber);
this.setExpiryDate(year, month);

if (!this.isCardNumber())
return false;
if (!this.isExpiryDate())
return false;

return true;
}
function getCardType() {
return this.cardtype;
}
function getExpiryDate() {
return this.month + "/" + this.year;
}
function isCardNumber() {
var argv = isCardNumber.arguments;
var argc = isCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
if (!this.luhnCheck())
return false;

for (var n = 0; n < this.len.size; n++)
if (cardnumber.toString().length == this.len[n]) {
for (var m = 0; m < this.rules.size; m++) {
var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
if (headdigit == this.rules[m])
return true;
}
return false;
}
return false;
}

function isExpiryDate() {
var argv = isExpiryDate.arguments;
var argc = isExpiryDate.arguments.length;

year = argc > 0 ? argv[0] : this.year;
month = argc > 1 ? argv[1] : this.month;

if (!isNum(year+""))
return false;
if (!isNum(month+""))
return false;
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
}

function isNum(argvalue) {
argvalue = argvalue.toString();

if (argvalue.length == 0)
return false;

for (var n = 0; n < argvalue.length; n++)
if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
return false;

return true;
}

function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

var CardNumber = argc > 0 ? argv[0] : this.cardnumber;

if (! isNum(CardNumber)) {
return false;
  }

var no_digit = CardNumber.length;
var oddoeven = no_digit & 1;
var sum = 0;

for (var count = 0; count < no_digit; count++) {
var digit = parseInt(CardNumber.charAt(count));
if (!((count & 1) ^ oddoeven)) {
digit *= 2;
if (digit > 9)
digit -= 9;
}
sum += digit;
}
if (sum % 10 == 0)
return true;
else
return false;
}

function makeArray(size) {
this.size = size;
return this;
}
function setCardNumber(cardnumber) {
this.cardnumber = cardnumber;
return this;
}
function setCardType(cardtype) {
this.cardtype = cardtype;
return this;
}
function setExpiryDate(year, month) {
this.year = year;
this.month = month;
return this;
}
function setLen(len) {
if (len.length == 0 || len == null)
len = "13,14,15,16,19";

var tmplen = len;
n = 1;
while (tmplen.indexOf(",") != -1) {
tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
n++;
}
this.len = new makeArray(n);
n = 0;
while (len.indexOf(",") != -1) {
var tmpstr = len.substring(0, len.indexOf(","));
this.len[n] = tmpstr;
len = len.substring(len.indexOf(",") + 1, len.length);
n++;
}
this.len[n] = len;
return this;
}
function setRules(rules) {
if (rules.length == 0 || rules == null)
rules = "0,1,2,3,4,5,6,7,8,9";
  
var tmprules = rules;
n = 1;
while (tmprules.indexOf(",") != -1) {
tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
n++;
}
this.rules = new makeArray(n);
n = 0;
while (rules.indexOf(",") != -1) {
var tmpstr = rules.substring(0, rules.indexOf(","));
this.rules[n] = tmpstr;
rules = rules.substring(rules.indexOf(",") + 1, rules.length);
n++;
}
this.rules[n] = rules;
return this;
}
</script>
</HEAD>
<BODY>

<form name="ThisForm">
Card Number: <input name="CardNumber" size="16" maxlength="19"><br>   
</select>
<br>
Expiration Date: Month
<select name="ExpMon">
<option value="1" selected>1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
<option value="10">10
<option value="11">11
<option value="12">12
</select>
Year <input name="ExpYear" size="2" maxlength="2"><br>
<input type="button" value="Check" onclick="CheckCardNumber(this.form)"><br>
</form>

This post has been edited by Dormilich: 03 September 2012 - 03:43 AM
Reason for edit:: please use [code] [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: anyone please help me

#2 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: anyone please help me

Posted 03 September 2012 - 03:38 AM

1) You need to use code tags when posting code.

2) What, exactly, are you looking for? Are you asking for help on how to verify the input? I'm not reading through all of that (unformatted) code to try to figure out what problem you're having. You need to state it, specifically and clearly.

Edit
This is clearly a homework-related problem. From one of the duplicates of this thread:

Quote

Create a page which allows a user to enter a credit card number and expiry date into a text field. When the user clicks a button, the page should check if the credit card number and expiry date is valid. A valid credit card number has 16 digits. A expiry date is valid if it has a 2 digit month followed by a 2 digit year. The month must be between 01 and 12 and the month and year must be greater than now.

Hint:
var d = new Date();
alert(d.getFullYear()); //displays 2012
alert(d.getMonth()); //displays the month number where january is 0 and december is 11

please anyone help me??


I'm assuming what you have above is what you have done so far.
What problems are you running into, and what are you having trouble understanding?

This post has been edited by Mina-no-Hime: 03 September 2012 - 03:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: anyone please help me

Posted 03 September 2012 - 03:58 AM

I've taken the liberty of giving the Javascript part of your code proper indentation. It'll make your life much easier if you start indenting when you program. In fact, one major problem can be spotted easily simply by merit of proper indentation.

var Cards = new makeArray(8);
Cards[0] = new CardType;
var MasterCard = Cards[0]= new CardType();

function CheckCardNumber(form) {
	var tmpyear;
	if (form.CardNumber.value.length == 0) {
		alert("Please enter a Card Number.");
		form.CardNumber.focus();
		return;
	}
	if (form.ExpYear.value.length == 0) {
		alert("Please enter the Expiration Year.");
		form.ExpYear.focus();
		return;
	}
	if (form.ExpYear.value > 96)
		tmpyear = "19" + form.ExpYear.value;
	else if (form.ExpYear.value < 12)
		tmpyear = "20" + form.ExpYear.value;
	else {
		alert("The Expiration Year is not valid.");
		return;
	}
	tmpmonth = form.ExpMon.options[form.ExpMon.selectedIndex].value;

	if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
		alert("This card has already expired.");
		return;
	}
	card = form.CardType.options[form.CardType.selectedIndex].value;
	var retval = eval(card + ".checkCardNumber(\"" + form.CardNumber.value +
		"\", " + tmpyear + ", " + tmpmonth + ");");
	cardname = "";
	if (retval)
		alert("This card number appears to be valid.");
	else {
		for (var n = 0; n < Cards.size; n++) {
			if (Cards[n].checkCardNumber(form.CardNumber.value, tmpyear, tmpmonth)) {
				cardname = Cards[n].getCardType();
				break;
			}
			else {
				alert("This card number is not valid.");
			}
		}
	}

function checkCardNumber() {
	var argv = checkCardNumber.arguments;
	var argc = checkCardNumber.arguments.length;
	var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
	var year = (argc > 1) ? argv[1] : this.year;
	var month = (argc > 2) ? argv[2] : this.month;

	this.setCardNumber(cardnumber);
	this.setExpiryDate(year, month);

	if (!this.isCardNumber())
		return false;
	if (!this.isExpiryDate())
		return false;

	return true;
}

function getCardType() {
	return this.cardtype;
}

function getExpiryDate() {
	return this.month + "/" + this.year;
}

function isCardNumber() {
	var argv = isCardNumber.arguments;
	var argc = isCardNumber.arguments.length;
	var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
	if (!this.luhnCheck())
		return false;

	for (var n = 0; n < this.len.size; n++)
		if (cardnumber.toString().length == this.len[n]) {
			for (var m = 0; m < this.rules.size; m++) {
				var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
				if (headdigit == this.rules[m])
					return true;
			}
			return false;
		}
	
	return false;
}

function isExpiryDate() {
	var argv = isExpiryDate.arguments;
	var argc = isExpiryDate.arguments.length;

	year = argc > 0 ? argv[0] : this.year;
	month = argc > 1 ? argv[1] : this.month;

	if (!isNum(year+""))
		return false;
	if (!isNum(month+""))
		return false;
	today = new Date();
	expiry = new Date(year, month);
	if (today.getTime() > expiry.getTime())
		return false;
	else
		return true;
}

function isNum(argvalue) {
	argvalue = argvalue.toString();

	if (argvalue.length == 0)
		return false;

	for (var n = 0; n < argvalue.length; n++)
		if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
			return false;

	return true;
}

function luhnCheck() {
	var argv = luhnCheck.arguments;
	var argc = luhnCheck.arguments.length;

	var CardNumber = argc > 0 ? argv[0] : this.cardnumber;

	if (! isNum(CardNumber)) {
		return false;
	}

	var no_digit = CardNumber.length;
	var oddoeven = no_digit & 1;
	var sum = 0;

	for (var count = 0; count < no_digit; count++) {
		var digit = parseInt(CardNumber.charAt(count));
		if (!((count & 1) ^ oddoeven)) {
			digit *= 2;
			if (digit > 9)
				digit -= 9;
		}
		sum += digit;
	}
	if (sum % 10 == 0)
		return true;
	else
		return false;
}

function makeArray(size) {
	this.size = size;
	return this;
}

function setCardNumber(cardnumber) {
	this.cardnumber = cardnumber;
	return this;
}

function setCardType(cardtype) {
	this.cardtype = cardtype;
	return this;
}

function setExpiryDate(year, month) {
	this.year = year;
	this.month = month;
	return this;
}

function setLen(len) {
	if (len.length == 0 || len == null)
		len = "13,14,15,16,19";

	var tmplen = len;
	n = 1;
	while (tmplen.indexOf(",") != -1) {
		tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
		n++;
	}
	this.len = new makeArray(n);
	n = 0;
	while (len.indexOf(",") != -1) {
		var tmpstr = len.substring(0, len.indexOf(","));
		this.len[n] = tmpstr;
		len = len.substring(len.indexOf(",") + 1, len.length);
		n++;
	}
	this.len[n] = len;
	return this;
}

function setRules(rules) {
	if (rules.length == 0 || rules == null)
		rules = "0,1,2,3,4,5,6,7,8,9";
  
	var tmprules = rules;
	n = 1;
	while (tmprules.indexOf(",") != -1) {
		tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
		n++;
	}
	this.rules = new makeArray(n);
	n = 0;
	while (rules.indexOf(",") != -1) {
		var tmpstr = rules.substring(0, rules.indexOf(","));
		this.rules[n] = tmpstr;
		rules = rules.substring(rules.indexOf(",") + 1, rules.length);
		n++;
	}
	this.rules[n] = rules;
	return this;
}

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1