How to validate SSN ( Social Security Number ) using Regular Expression | C ++ program to validate the SSN ( Social Security Number ) using Regular Expression ; Function to validate the SSN ( Social Security Number ) ; Regex to check valid SSN ( Social Security Number ) ; If the SSN ( Social Security Number ) is empty return false ; Return true if the SSN ( Social Security Number ) matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidSSN ( string str ) { const regex pattern ( " ^ ( ? ! 666 ▁ 000 ▁ 9 \\ d { 2 } ) " " \\ d { 3 } - ( ? !00 ) " " \\ d { 2 } - ( ? !0 { 4 } ) \\ d { 4 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "856-45-6789" ; cout << isValidSSN ( str1 ) << endl ; string str2 = "000-45-6789" ; cout << isValidSSN ( str2 ) << endl ; string str3 = "856-452-6789" ; cout << isValidSSN ( str3 ) << endl ; string str4 = "856-45-0000" ; cout << isValidSSN ( str4 ) << endl ; return 0 ; }