### Development Setup Source: https://www.npmjs.com/package/card-validator/index Installs Node.js using nvm and installs project dependencies. All testing dependencies are installed automatically. ```bash nvm install npm install ``` -------------------------------- ### Install Package Source: https://www.npmjs.com/package/card-validator/index Installs the card-validator package using npm. ```bash npm i card-validator ``` -------------------------------- ### Install card-validator Source: https://www.npmjs.com/package/card-validator/index Installs the card-validator package using npm. This is the standard way to add the package to your Node.js project. ```bash npm install card-validator ``` -------------------------------- ### Run Test Suite Source: https://www.npmjs.com/package/card-validator/index Executes the test suite for the card-validator package. ```bash npm test ``` -------------------------------- ### Card Number Validation Scenarios Source: https://www.npmjs.com/package/card-validator/index Illustrates various input values for credit card numbers and their corresponding validation outputs, including card type, potential validity, and final validity. This helps in understanding how the validator behaves with different inputs and states. ```APIDOC Input | card.type | isPotentiallyValid | isValid | Render Invalid UI | Allow Submit --|--|--|--|--|-- `''` | `null` | **true** | false | no | no `'6'` | `null` | **true** | false | no | no `'60'` | `'discover'` | **true** | false | no | no `'601'` | `'discover'` | **true** | false | no | no `'6011'` | `'discover'` | **true** | false | no | no `'601'` | `'discover'` | **true** | false | no | no `'60'` | `'discover'` | **true** | false | no | no `'6'` | `null` | **true** | false | no | no `''` | `null` | **true** | false | no | no `'x'` | `null` | false | false | **yes** | no `''` | `null` | **true** | false | no | no `'4'` | `'visa'` | **true** | false | no | no `'41'` | `'visa'` | **true** | false | no | no `'411'` | `'visa'` | **true** | false | no | no `'4111111111111111'` | `'visa'` | **true** | **true** | no | **yes** `'411x'` | `null` | false | false | **yes** | no ``` -------------------------------- ### Basic Usage of card-validator Source: https://www.npmjs.com/package/card-validator/index Demonstrates how to use the card-validator package in a CommonJS environment (like Node.js or with bundlers like Webpack/Browserify). It shows how to validate a card number and access detected card type information. ```javascript var valid = require("card-validator"); var numberValidation = valid.number("4111"); if (!numberValidation.isPotentiallyValid) { renderInvalidCardNumber(); } if (numberValidation.card) { console.log(numberValidation.card.type); // 'visa' } ``` -------------------------------- ### Add Custom Card Brands Source: https://www.npmjs.com/package/card-validator/index Allows adding custom card brands to the validator by utilizing the `credit-card-type` module. The `addCard` method requires an object with properties like `niceType`, `type`, `patterns`, `gaps`, `lengths`, and `code` details. ```javascript valid.creditCardType.addCard({ niceType: "NewCard", type: "new-card", patterns: [1234], gaps: [4, 8, 12], lengths: [16], code: { name: "CVV", size: 3, }, }); ``` -------------------------------- ### Validate Postal Code Source: https://www.npmjs.com/package/card-validator/index Validates a postal code. It ignores leading/trailing whitespace, checks for a minimum length (default 3 characters), and verifies the first 3 characters are alphanumeric. An optional `options` object with `minLength` can override the default. Returns an object indicating potential and actual validity. ```javascript valid.postalCode(value: string, [options: object]): object // Example with custom minLength: // valid.postalCode('123', {minLength: 5}); /* { isPotentiallyValid: true, isValid: true } */ /* // Example output for invalid input: { isPotentiallyValid: true, isValid: false } */ ``` -------------------------------- ### Validate Cardholder Name Source: https://www.npmjs.com/package/card-validator/index Validates a cardholder's name. The validation checks if the name is a non-empty string and does not resemble a card number. Names consisting solely of numbers, hyphens, and spaces are considered too card-like. Names exceeding 255 characters are invalid. ```javascript valid.cardholderName(value: string): object // Example of a valid name valid.cardholderName('John Doe'); // Example of a name that is too card-like valid.cardholderName('1234567890'); // Example of a name that is too long valid.cardholderName('a'.repeat(256)); /* Example Response for valid name: { isPotentiallyValid: true, isValid: true } Example Response for too card-like name: { isPotentiallyValid: true, isValid: false } Example Response for name too long: { isPotentiallyValid: false, isValid: false } */ ``` -------------------------------- ### Validate Expiration Month Source: https://www.npmjs.com/package/card-validator/index Validates the expiration month of a credit card. Accepts 1 or 2 digit months (e.g., '1', '01', '10'). Returns an object indicating if the month is valid for the current year, potentially valid, and actually valid. ```javascript valid.expirationMonth(value: string): object /* { isValidForThisYear: false, isPotentiallyValid: true, isValid: true } */ ``` -------------------------------- ### Validate Expiration Date Source: https://www.npmjs.com/package/card-validator/index Validates a credit card's expiration date. It can parse various string and object formats for month and year. The `maxElapsedYear` parameter sets the future validity window, defaulting to 19 years. Returns an object indicating potential validity, actual validity, and parsed month/year. ```javascript valid.expirationDate(value: string|object, maxElapsedYear: integer): object // Example Input Formats: // '10/19', '10 / 19', '1019', '10 19' -> {month: '10', year: '19'} // '10/2019', '10 / 2019', '102019', '10 2019', '10 19' -> {month: '10', year: '2019'} // '2019-10' -> {month: '10', year: '2019'} // {month: '01', year: '19'}, {month: '1', year: '19'}, {month: 1, year: 19} -> {month: '01', year: '19'} // {month: '01', year: '2019'}, {month: '1', year: '2019'}, {month: 1, year: 2019} -> {month: '01', year: '2019'} /* { isPotentiallyValid: true, isValid: true, month: '10', year: '2016' } */ ``` -------------------------------- ### Validate Expiration Year Source: https://www.npmjs.com/package/card-validator/index Validates the expiration year of a credit card. Accepts 2 or 4 digit years (e.g., '16', '2016'). The `maxElapsedYear` parameter determines the future validity window, defaulting to 19 years. Returns an object indicating if the year is the current year, potentially valid, and actually valid. ```javascript valid.expirationYear(value: string, maxElapsedYear: integer): object /* { isCurrentYear: false, isPotentiallyValid: true, isValid: true } */ ``` -------------------------------- ### Validate CVV Source: https://www.npmjs.com/package/card-validator/index Validates the Card Verification Value (CVV) of a credit card. By default, it checks for a numeric string of 3 characters. The `maxLength` parameter can be adjusted, typically to 4 for American Express cards. Returns an object indicating potential and actual validity. ```javascript valid.cvv(value: string, maxLength: integer): object /* { isPotentiallyValid: true, isValid: true } */ ``` -------------------------------- ### Validate Credit Card Number Source: https://www.npmjs.com/package/card-validator/index Validates a credit card number, returning details about the card type and its validity. Supports options to override default Luhn validation for UnionPay cards and to specify a maximum length. If no valid card type can be determined, the 'card' field will be null. ```javascript valid.number(value: string, [options: object]): object // Example with default options valid.number('4111111111111111'); // Example overriding UnionPay Luhn validation valid.number('', {luhnValidateUnionPay: true}); // Example overriding maxLength valid.number('', {maxLength: 16}); /* Example Response: { card: { niceType: 'American Express', type: 'american-express', gaps: [4, 10], lengths: [15], code: {name: 'CID', size: 4} }, isPotentiallyValid: true, isValid: true } */ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.