Skip to content

Commit 7731b99

Browse files
committed
Added Credit Card Validation
1 parent 2dbdde9 commit 7731b99

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ npm install validate-functions
1515
- **Email Validation**: Ensures that email addresses conform to standard formats.
1616
- **SSN Validation**: Validates U.S. Social Security Numbers based on predefined rules.
1717
- **Aadhaar Validation**: Checks the validity of Indian Aadhaar numbers using specific patterns.
18+
- **Credit Card**: The credit card validation uses the Luhn algorithm to verify the validity of credit card numbers structurally.
1819

1920

2021
Usage Details available at [here](./USAGE_DETAILS.md).

USAGE_DETAILS.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ import { isValid_Aadhaar_Number } from 'validate-functions/aadhaar';
2525

2626
const aadhaarNumber = '1234 5678 9012';
2727
console.log(isValid_Aadhaar_Number(aadhaarNumber)); // Output: true if valid, false otherwise
28-
```
28+
```
29+
30+
- **isValid_Credit_Card**
31+
32+
```typescript
33+
import { isValid_Credit_Card } from 'validate-functions/credit-card';
34+
35+
const creditCardNumber = '4539 1488 0343 6467'; // Example of a valid Visa card number
36+
console.log(isValid_Credit_Card(creditCardNumber)); // Output: true if valid, false otherwise
37+
```

src/__tests__/credit-card.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { isValid_Credit_Card } from '../credit-card';
2+
3+
describe('Credit Card Validation', () => {
4+
it('should return true for a valid credit card number', () => {
5+
const validCardNumber = '4539 1488 0343 6467'; // Example of a valid Visa card number
6+
expect(isValid_Credit_Card(validCardNumber)).toBe(true);
7+
});
8+
9+
it('should return false for an invalid credit card number', () => {
10+
const invalidCardNumber = '1234 5678 9012 3456'; // Example of an invalid card number
11+
expect(isValid_Credit_Card(invalidCardNumber)).toBe(false);
12+
});
13+
14+
it('should return true for a valid credit card number without spaces', () => {
15+
const validCardNumberNoSpaces = '4539148803436467';
16+
expect(isValid_Credit_Card(validCardNumberNoSpaces)).toBe(true);
17+
});
18+
19+
it('should return false for an empty string', () => {
20+
const emptyCardNumber = '';
21+
expect(isValid_Credit_Card(emptyCardNumber)).toBe(false);
22+
});
23+
});

src/credit-card.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* A credit card is essentially a means of borrowing money that is accompanied by interest and sometimes fees.
3+
* Validation is based on Luhn algorithm
4+
* @param cardNumber
5+
* @returns
6+
*/
7+
export const isValid_Credit_Card = (cardNumber: string): boolean => {
8+
const sanitized = cardNumber.replace(/\D/g, '');
9+
let sum = 0;
10+
let shouldDouble = false;
11+
12+
if(!sanitized) return false;
13+
14+
for (let i = sanitized.length - 1; i >= 0; i--) {
15+
let digit = parseInt(sanitized[i], 10);
16+
17+
if (shouldDouble) {
18+
digit *= 2;
19+
if (digit > 9) digit -= 9;
20+
}
21+
22+
sum += digit;
23+
shouldDouble = !shouldDouble;
24+
}
25+
26+
return sum % 10 === 0;
27+
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './aadhaar';
2+
export * from './credit-card';
23
export * from './email';
34
export * from './ssn';

0 commit comments

Comments
 (0)