File tree Expand file tree Collapse file tree 5 files changed +62
-1
lines changed
Expand file tree Collapse file tree 5 files changed +62
-1
lines changed Original file line number Diff line number Diff 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
2021Usage Details available at [ here] ( ./USAGE_DETAILS.md ) .
Original file line number Diff line number Diff line change @@ -25,4 +25,13 @@ import { isValid_Aadhaar_Number } from 'validate-functions/aadhaar';
2525
2626const aadhaarNumber = ' 1234 5678 9012' ;
2727console .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+ ```
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 11export * from './aadhaar' ;
2+ export * from './credit-card' ;
23export * from './email' ;
34export * from './ssn' ;
You can’t perform that action at this time.
0 commit comments