Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript exercise that requires to create credit card input fields validation functions.
/**
* @fileoverview Utils Interface credit card validation methods.
* @author (Wind Tammer)
*/
utils = {};
/** @type {Object<Function>} */
utils.creditcard = {};
/**
* Checks whether credit card number is valid.
* @param {string} cardNumber Credit card number.
* @return {Boolean}
*/
utils.creditcard.validateNumber = function(cardNumber) {
// Function has to return boolean identifying whether credit
// card number contains exactly 16 digits.
};
/**
* Checks whether credit card security code is valid.
* @param {string} securityCode Credit card security.
* @return {Boolean}
*/
utils.creditcard.validateSecurityCode = function(securityCode) {
// Function has to return boolean identifying whether credit
// card security code (CVV/CVC) contains exactly 3 digits.
};
/**
* Checks whether credit card name is valid.
* @param {string} name Credit card owner name.
* @return {Boolean}
*/
utils.creditcard.validateName = function(name) {
// Function has to return boolean identifying whether name on
// the credit card contains at least 2 words with no digits.
};
/**
* Checks whether credit card expiration date is valid.
* @param {string} expirationDate Credit card expiration date in
* YYYY-MM format.
* @return {Boolean}
*/
utils.creditcard.validateDate = function(expirationDate) {
// Function has to return boolean identifying whether credit
// card expiration date is at least next month. If today is April,
// expiration date has to be at least May.
};
/**
* @fileoverview Super simple Unit tests. If any outputs
* 'false' function is not working as planned.
* @author (Wind Tammer)
*/
console.log(utils.creditcard.validateNumber('4111111111111111') === true);
console.log(utils.creditcard.validateNumber('2312312312') === false);
console.log(utils.creditcard.validateNumber('411a343432dasd') === false);
console.log(utils.creditcard.validateNumber('4111 1111 1111 1111') === false);
console.log(utils.creditcard.validateNumber(4111111111111111) === false);
console.log(utils.creditcard.validateSecurityCode('09') === false);
console.log(utils.creditcard.validateSecurityCode('A12') === false);
console.log(utils.creditcard.validateSecurityCode('1 2 3') === false);
console.log(utils.creditcard.validateSecurityCode('347') === true);
console.log(utils.creditcard.validateSecurityCode(347) === false);
console.log(utils.creditcard.validateSecurityCode('347234234') === false);
console.log(utils.creditcard.validateName('Nik Sumeiko') === true);
console.log(utils.creditcard.validateName('NikSumeiko') === false);
console.log(utils.creditcard.validateName('Nik Sumeiko II') === true);
console.log(utils.creditcard.validateName('Nik Sumeiko 2') === false);
console.log(utils.creditcard.validateName(' ') === false);
console.log(utils.creditcard.validateDate('2014-04') === false);
console.log(utils.creditcard.validateDate('2014-07') === true);
console.log(utils.creditcard.validateDate('201404') === false);
console.log(utils.creditcard.validateDate(201407) === false);
console.log(utils.creditcard.validateDate('') === false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment