-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathco_credit.js
165 lines (134 loc) · 4.8 KB
/
co_credit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"use strict";
/*
New Perspectives on HTML5, CSS3, and JavaScript 6th Edition
Tutorial 13
Review Assignment
Credit Card Form Script
Author: Hesbon Osoro
Date: 12/10/22
Filename: co_credit.js
Function List
=============
runSubmit()
Runs validation tests when the submit button is clicked
validateCVC()
Validates the credit card CVC number
validateDate()
Validates that the user has entered a valid expiration date for the credit card
validateYear()
Validates that the user has selected the expiration year of the credit card
validateNumber()
Validates that the user has entered a valid and legitimate card number
validateCredit()
Validates that the user has selected a credit card type
validateName()
Validates that the user has specified the name on the credit card
sumDigits(numStr)
Sums the digits characters in a text string
luhn(idNum)
Returns true of idNum satisfies the Luhn Algorithm
*/
/* Event Listeners */
window.addEventListener("load", function () {
var orderData = location.search.slice(1);
orderData = orderData.replace(/\+/g, " ");
orderData = decodeURIComponent(orderData);
var orderFields = orderData.split(/[&=]/g);
document.getElementById("modelName").value = orderFields[3];
document.getElementById("modelQty").value = orderFields[5];
document.getElementById("orderCost").value = orderFields[7];
document.getElementById("shippingType").value = orderFields[9];
document.getElementById("shippingCost").value = orderFields[13];
document.getElementById("subTotal").value = orderFields[15];
document.getElementById("salesTax").value = orderFields[17];
document.getElementById("cartTotal").value = orderFields[19];
});
window.addEventListener("load", function () {
document.getElementById("subButton").onclick = runSubmit;
document.getElementById("cardHolder").oninput = validateName;
document.getElementById("cardNumber").oninput = validateNumber;
document.getElementById("expDate").oninput = validateDate;
document.getElementById("cvc").oninput = validateCVC;
});
function runSubmit() {
validateName();
validateCredit();
validateNumber();
validateDate();
validateCVC();
}
function validateDate() {
var expDate = document.getElementById("expDate");
if (expDate.validity.valueMissing) {
expDate.setCustomValidity("Enter the expiration date");
} else if (/^(0[1-9]|1[0-2])\/20[12]\d$/.test(expDate.value) === false) {
expDate.setCustomValidity("Enter a valid expiration date");
} else {
expDate.setCustomValidity("");
}
}
/* Functions already provided in the file */
function validateName() {
var cardName = document.getElementById("cardHolder");
if (cardName.validity.valueMissing) {
cardName.setCustomValidity("Enter the card holder");
} else {
cardName.setCustomValidity("");
}
}
function validateCredit() {
var creditCard = document.forms.credit.elements.company[0];
if (creditCard.validity.valueMissing) {
creditCard.setCustomValidity("Select your credit card");
} else {
creditCard.setCustomValidity("");
}
}
function validateNumber() {
var cardNumber = document.getElementById("cardNumber");
if (cardNumber.validity.valueMissing) {
cardNumber.setCustomValidity("Enter your card number");
} else if (cardNumber.validity.patternMismatch) {
cardNumber.setCustomValidity("Enter a valid card number");
} else if (luhn(cardNumber.value) === false) {
cardNumber.setCustomValidity("Enter a legitimate card number");
} else {
cardNumber.setCustomValidity("");
}
}
function validateCVC() {
var cardCVC = document.getElementById("cvc");
var creditCard = document.querySelector(
'input[name="company"]:checked'
).value;
if (cardCVC.validity.valueMissing) {
cardCVC.setCustomValidity("Enter your code CVC number");
} else if (creditCard === "amex" && /^\d{4}$/.test(cardCVC.value) === false) {
cardCVC.setCustomValidity("Enter a 4-digit CVC number");
} else if (creditCard !== "amex" && /^\d{3}$/.test(cardCVC.value) === false) {
cardCVC.setCustomValidity("Enter a 3-digit CVC number");
} else {
cardCVC.setCustomValidity("");
}
}
function sumDigits(numStr) {
var digitTotal = 0;
for (var i = 0; i < numStr.length; i++) {
digitTotal += parseInt(numStr.charAt(i));
}
return digitTotal;
}
function luhn(idNum) {
var string1 = "";
var string2 = "";
// Retrieve the odd-numbered digits
for (var i = idNum.length - 1; i >= 0; i -= 2) {
string1 += idNum.charAt(i);
}
// Retrieve the even-numbered digits and double them
for (var i = idNum.length - 2; i >= 0; i -= 2) {
string2 += 2 * idNum.charAt(i);
}
// Return whether the sum of the digits is divisible by 10
return sumDigits(string1 + string2) % 10 === 0;
}