The isCreditCardValid
function checks if the input string represents a valid credit card number using the Luhn algorithm. It returns true
if the credit card number is valid, and false
otherwise.
The function can be imported using ES6 syntax from the "multiform-validator" package:
import { isCreditCardValid } from 'multiform-validator';
Alternatively, you can import the function using CommonJS syntax with require
(Node.js):
const { isCreditCardValid } = require('multiform-validator');
The function takes one parameter:
cardNumber
(string) - The input string representing the credit card number to validate.// Example 1 - Valid credit card number with spaces
const result1 = isCreditCardValid('6062 8226 8644 9791');
console.log(result1); // true
// Example 2 - Valid credit card number without spaces
const result2 = isCreditCardValid('6062822686449791');
console.log(result2); // true
// Example 3 - Invalid credit card number (does not pass the Luhn algorithm)
const result3 = isCreditCardValid('1234 5678 9012 3456');
console.log(result3); // false
The function expects the input value to be passed as a string representing a credit card number. It removes any non-digit characters and applies the Luhn algorithm to validate the credit card number. If the input is not a string, the function throws an error.