Created
January 14, 2023 20:45
-
-
Save onovogodwinprosperity/268029e9c5093cdcde1390104675911b to your computer and use it in GitHub Desktop.
Validation of Payment card (Visa and Master Card) Using Regular Expression
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
// below are the indept explanation of some of the signs i use:- | |
// (/) sign inside bracket alert the js engine that am about to declare a regular expression | |
// (^) is telling the JS engine again that what i need as the first digit is (5 if its master card, 4 if its Visa card as i declare it differently ) | |
// ($) This sign tells the JS engine that am done with the parttern i want to use and validate any kind of input i need in my application | |
//============================================================== ====================================================== | |
// MASTER CARD VALIDATION | |
// Its known very well that every payment cards has there specifi numbers of digits | |
// Master Card has a Total 16 digits | |
// Valid Master card digits always start with five (5) digit | |
//============================================================== ====================================================== | |
const masterCardValidation = /^5[1-5]{1}[0-9]{2}[\s]?[0-9]{4}[\s]?[0-9]{4}[\s]?[0-9]{4}$/.test("5433 4552 2233 4645") | |
console.log(masterCardValidation) | |
//============================================================== ====================================================== | |
// the above is the mater card validated with minimum number of 16 digits number | |
// the first number going to be inserted must be five (5), | |
// that is what (^5) perttern above indicated code before other codes like [1-5]{1} indicating that {2} it must be within 1-5 range | |
// the console here calles the regular expression in terminal as to show result | |
// this is the mastercard validation that is cut shorter and more understandible | |
const masterCardValidation1 = /^4[0-9]{4}(?:[0-9]{12})?$/.test("42244887492234558") | |
console.log(masterCardValidation1) | |
// VISA CARD VALIDATION | |
// in Visa card validation, its well known that the digits of Visa card always begins with four (4) and stops in thirtheen (13) or sixthen | |
// Below is the example of the one that stops in sixten | |
const visaCardValidation = /^4[1-5]{1}[0-9]{2}[\s]?[0-9]{4}[\s]?[0-9]{4}[\s]?[0-9]{4}$/.test("4445 8623 4655 5668"); | |
console.log(visaCardValidation) | |
// again below are the one that stops in 13 precisely | |
const visaCardValidation2 = /^4[0-9]{13}$/.test("48834678434434"); | |
console.log(visaCardValidation2) | |
// am Onovo Godwin (Backend) === Learnable | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment