Last active
January 15, 2023 02:35
-
-
Save VictorJoshuaC/73dbd311d4043f1ac6bdbce00c67f621 to your computer and use it in GitHub Desktop.
A simple JavaScript program that validates credit card numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This program uses the test() method of the regular expression object | |
| // to check if the card number matches the pattern of the respective card type. | |
| // The program will check for a match against a regular expression for Visa, Mastercard, | |
| // Verve and Discover card numbers in that order. | |
| // If the card number is valid for a specific card type the respective | |
| // message will be displayed and the checking will stop. | |
| // pls uncomment this one by one to display | |
| // const cardNumber = "4011211119114313"; | |
| const cardNumber = "5110762060017101"; | |
| // const cardNumber = "3471211119113123"; | |
| // const cardNumber = "6011211119113123"; | |
| const visaRegEx = /^4[0-9]{12}(?:[0-9]{3})?$/; | |
| const mastercardRegEx = /^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/; | |
| const verveRegEx = /^3[47][0-9]{13}/; | |
| const discoverRegEx = /^6(?:011|5[0-9]{2})[0-9]{12}$/; | |
| if (visaRegEx.test(cardNumber)) { | |
| console.log("This is a valid VISA card number."); | |
| } else if (mastercardRegEx.test(cardNumber)) { | |
| console.log("This is a valid MASTERCARD number."); | |
| } else if (verveRegEx.test(cardNumber)) { | |
| console.log("This is a valid VERVE card number."); | |
| } else if (discoverRegEx.test(cardNumber)) { | |
| console.log("This is a valid DISCOVER card number."); | |
| } else { | |
| console.log("This is not a valid card number."); | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
up we go