How to validate Visa Card number using Regular Expression | Java program to validate Visa Card number using regular expression ; Function to validate Visa Card number . using regular expression . ; Regex to check valid . Visa Card number ; Compile the ReGex ; If the string is empty return false ; Find match between given string and regular expression using Pattern . matcher ( ) ; Return if the string matched the ReGex ; Driver code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import java . util . regex . * ; class GFG { public static boolean isValidVisaCardNo ( String str ) { String regex = " ^ 4[0-9 ] { 12 } ( ? : [0-9 ] { 3 } ) ? $ " ; Pattern p = Pattern . compile ( regex ) ; if ( str == null ) { return false ; } Matcher m = p . matcher ( str ) ; return m . matches ( ) ; } public static void main ( String args [ ] ) { String str1 = "4155279860457" ; System . out . println ( isValidVisaCardNo ( str1 ) ) ; String str2 = "4155279860457201" ; System . out . println ( isValidVisaCardNo ( str2 ) ) ; String str3 = "4155279" ; System . out . println ( isValidVisaCardNo ( str3 ) ) ; String str4 = "6155279860457" ; System . out . println ( isValidVisaCardNo ( str4 ) ) ; String str5 = "415a27 # # 60457" ; System . out . println ( isValidVisaCardNo ( str5 ) ) ; } }