Skip to content

Commit 85b0571

Browse files
Bhargavishnugithub-actions
and
github-actions
authored
chore: Strings: Credit Card Validation (#830)
* Add credit card number validator - Validates the credit card number based on Luhn algorithm * Test Cases: ValidateCreditCard * Auto-update DIRECTORY.md * Fix: Spell check * Add references and move const inside function * Add comments * Fix trailing spaces * Add short description * Remove trailing spaces Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 64920bf commit 85b0571

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js)
311311
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.js)
312312
* [ScrambleStrings](https://github.com/TheAlgorithms/Javascript/blob/master/String/ScrambleStrings.js)
313+
* [ValidateCreditCard](https://github.com/TheAlgorithms/Javascript/blob/master/String/ValidateCreditCard.js)
313314
* [ValidateEmail](https://github.com/TheAlgorithms/Javascript/blob/master/String/ValidateEmail.js)
314315

315316
## Timing-Functions

String/ValidateCreditCard.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Validate a given credit card number
3+
*
4+
* The core of the validation of credit card numbers is the Luhn algorithm.
5+
*
6+
* The validation sum should be completely divisible by 10 which is calculated as follows,
7+
* every first digit is added directly to the validation sum.
8+
* For every second digit in the credit card number, the digit is multiplied by 2.
9+
* If the product is greater than 10 the digits of the product are added.
10+
* This resultant digit is considered for the validation sum rather than the digit itself.
11+
*
12+
* Ref: https://www.geeksforgeeks.org/luhn-algorithm/
13+
*/
14+
15+
const luhnValidation = (creditCardNumber) => {
16+
let validationSum = 0
17+
creditCardNumber.split('').forEach((digit, index) => {
18+
let currentDigit = parseInt(digit)
19+
if (index % 2 === 0) {
20+
// Multiply every 2nd digit from the left by 2
21+
currentDigit *= 2
22+
// if product is greater than 10 add the individual digits of the product to get a single digit
23+
if (currentDigit > 9) {
24+
currentDigit %= 10
25+
currentDigit += 1
26+
}
27+
}
28+
validationSum += currentDigit
29+
})
30+
31+
return validationSum % 10 === 0
32+
}
33+
34+
const validateCreditCard = (creditCardString) => {
35+
const validStartSubString = ['4', '5', '6', '37', '34', '35'] // Valid credit card numbers start with these numbers
36+
37+
if (typeof creditCardString !== 'string') {
38+
throw new TypeError('The given value is not a string')
39+
}
40+
41+
const errorMessage = `${creditCardString} is an invalid credit card number because `
42+
if (isNaN(creditCardString)) {
43+
throw new TypeError(errorMessage + 'it has nonnumerical characters.')
44+
}
45+
const creditCardStringLength = creditCardString.length
46+
if (!((creditCardStringLength >= 13) && (creditCardStringLength <= 16))) {
47+
throw new Error(errorMessage + 'of its length.')
48+
}
49+
if (!validStartSubString.some(subString => creditCardString.startsWith(subString))) {
50+
throw new Error(errorMessage + 'of its first two digits.')
51+
}
52+
if (!luhnValidation(creditCardString)) {
53+
throw new Error(errorMessage + 'it fails the Luhn check.')
54+
}
55+
56+
return true
57+
}
58+
59+
export { validateCreditCard }
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { validateCreditCard } from '../ValidateCreditCard'
2+
3+
describe('Validate credit card number', () => {
4+
it('should throw error if card number is boolean', () => {
5+
const invalidCC = true
6+
expect(() => validateCreditCard(invalidCC)).toThrow(
7+
'The given value is not a string'
8+
)
9+
})
10+
it('returns true if the credit card number is valid', () => {
11+
const validCreditCard = '4111111111111111'
12+
const validationResult = validateCreditCard(validCreditCard)
13+
expect(validationResult).toBe(true)
14+
})
15+
it('should throw an error on non-numeric character in given credit card number', () => {
16+
const nonNumericCCNumbers = ['123ABCDEF', 'ABCDKDKD', 'ADS232']
17+
nonNumericCCNumbers.forEach(nonNumericCC => expect(() => validateCreditCard(nonNumericCC)).toThrow(
18+
`${nonNumericCC} is an invalid credit card number because ` + 'it has nonnumerical characters.'
19+
))
20+
})
21+
it('should throw an error on credit card with invalid length', () => {
22+
const ccWithInvalidLength = ['41111', '4111111111111111111111']
23+
ccWithInvalidLength.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow(
24+
`${invalidCC} is an invalid credit card number because ` + 'of its length.'
25+
))
26+
})
27+
it('should throw an error on credit card with invalid start substring', () => {
28+
const ccWithInvalidStartSubstring = ['12345678912345', '23456789123456', '789123456789123', '891234567891234', '912345678912345', '31345678912345', '32345678912345', '33345678912345', '38345678912345']
29+
ccWithInvalidStartSubstring.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow(
30+
`${invalidCC} is an invalid credit card number because ` + 'of its first two digits.'
31+
))
32+
})
33+
it('should throw an error on credit card with luhn check fail', () => {
34+
const invalidCCs = ['411111111111111', '371211111111111', '49999999999999']
35+
invalidCCs.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow(
36+
`${invalidCC} is an invalid credit card number because ` + 'it fails the Luhn check.'
37+
))
38+
})
39+
})

0 commit comments

Comments
 (0)