Add another event listener for the window load event that runs different validation event handlers when the page is loaded by the browser. Add the following code to the anonymous function for load event:
Create the runSubmit() function that is run when the form is submitted. Within the function add commands to run the validateName(), validateCredit(), validateNumber(), validateDate(), and validateCVC() functions.
Create the validateDate() function. The purpose of this function is to validate the credit card expiration date stored in the expDate field. Within the function, insert an if-else structure that tests the following:
The remaining functions have already been entered for you.
"use strict";
/*
New Perspectives on HTML5, CSS3, and JavaScript 6th Edition
Tutorial 13
Review Assignment
Credit Card Form Script
Author:
Date:
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
*/
function runSubmit() {
}
function validateDate() {
}
function validateName() {
}
function validateCredit() {
}
function validateNumber() {
}
function validateCVC() {
}
function sumDigits(numStr) {
}
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;
}