Forked from niksumeiko/exercises.creditcard.js
Last active
September 23, 2015 12:10
-
-
Save WindTamer/11199270 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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. | |
| return /^\d{16}$/.test(cardNumber); | |
| }; | |
| /** | |
| * 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. | |
| return /^\d{3}$/.test(securityCode); | |
| }; | |
| /** | |
| * 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. | |
| return /^[a-z]{2,}(\s[a-z]{2,}){1,}$/i.test(name); | |
| }; | |
| /** | |
| * 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. | |
| var expirationDateArray = expirationDate.split('-', 2); | |
| var today = new Date(); | |
| var thisMonth = today.getUTCMonth() + 1; | |
| var thisYear = today.getFullYear(); | |
| var givenMonth = parseInt(expirationDateArray[1], 10); | |
| var givenYear = parseInt(expirationDateArray[0], 10); | |
| return(givenYear > thisYear) || (givenYear == thisYear) && (givenMonth > thisMonth); | |
| }; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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
I see you have finalised
utils.creditcard.validateDatemethod. This means we have finished the exercise successfully. Congratulations!From now on you will be able to validate credit cards, when you have a project that requires credit card validation on the client. Keep your creations under control and reuse them.