Skip to content

Commit

Permalink
added validate cvv /cvc feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshbalaji07 committed Jun 27, 2023
1 parent 8246a39 commit d30448c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,29 @@ function validateCreditCardNumber(cardNumber) {
return sum % 10 === 0;
}

function validateCVVORCVCCode(securityCode) {
// Remove any spaces or dashes from the security code
securityCode = securityCode.replace(/[^\d]/g, "");

// Check if the security code is empty or not a number
if (securityCode === "" || isNaN(securityCode)) {
return false;
}

// Check if the security code is 3 or 4 digits long
var validLengths = [3, 4];
if (!validLengths.includes(securityCode.length)) {
return false;
}

// The security code is valid
return true;
}

module.exports = {
isCreditCard,
isEncryptedToken,
detectCardType,
validateCreditCardNumber,
validateCVVORCVCCode,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "card-validator-utils",
"version": "1.0.4",
"version": "1.0.5",
"description": "Library package util for payment industry",
"main": "index.js",
"scripts": {
Expand Down
20 changes: 18 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
isEncryptedToken,
detectCardType,
validateCreditCardNumber,
validateCVVORCVCCode,
} = require("../index");

describe("Testing credit card Functionality", () => {
Expand Down Expand Up @@ -53,6 +54,21 @@ describe("Validate the card number", () => {
expect(validateCreditCardNumber("30569309025904")).toBe(true);
});

test("should return false for invalid card number", () => {});
expect(validateCreditCardNumber("42428974842424242")).toBe(false);
test("should return false for invalid card number", () => {
expect(validateCreditCardNumber("42428974842424242")).toBe(false);
});
});

describe("Validate the CVV/CVC number", () => {
test("should return true for valid CVV / CVC", () => {
expect(validateCVVORCVCCode("234")).toBe(true);
expect(validateCVVORCVCCode("2345")).toBe(true);
expect(validateCVVORCVCCode("8987")).toBe(true);
expect(validateCVVORCVCCode("098")).toBe(true);
});

test("should return false for invalid CVV / CVC", () => {
expect(validateCVVORCVCCode("67")).toBe(false);
expect(validateCVVORCVCCode("jhjjhh")).toBe(false);
});
});

0 comments on commit d30448c

Please sign in to comment.