From d30448c1e97cc892e8d87dbb975751df783412e5 Mon Sep 17 00:00:00 2001 From: vigneshbalaji07 Date: Tue, 27 Jun 2023 20:37:00 +0530 Subject: [PATCH] added validate cvv /cvc feature --- index.js | 20 ++++++++++++++++++++ package.json | 2 +- test/index.test.js | 20 ++++++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 62c1ab3..ab5e4c8 100644 --- a/index.js +++ b/index.js @@ -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, }; diff --git a/package.json b/package.json index d773f9f..218c137 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/index.test.js b/test/index.test.js index bc84038..410f373 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,6 +3,7 @@ const { isEncryptedToken, detectCardType, validateCreditCardNumber, + validateCVVORCVCCode, } = require("../index"); describe("Testing credit card Functionality", () => { @@ -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); + }); });