How to validate Visa Card number using Regular Expression | C ++ program to validate Visa Card number using Regular Expression ; Function to validate Visa Card number ; Regex to check valid Visa Card number ; If the Visa Card number is empty return false ; Return true if the Visa Card number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidVisaCardNo ( string str ) { const regex pattern ( " ^ 4[0-9 ] { 12 } ( ? : [0-9 ] { 3 } ) ? $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "4155279860457" ; cout << isValidVisaCardNo ( str1 ) << endl ; string str2 = "4155279860457201" ; cout << isValidVisaCardNo ( str2 ) << endl ; string str3 = "4155279" ; cout << isValidVisaCardNo ( str3 ) << endl ; string str4 = "6155279860457" ; cout << isValidVisaCardNo ( str4 ) << endl ; string str5 = "415a27 # # 60457" ; cout << isValidVisaCardNo ( str5 ) << endl ; return 0 ; }