How to validate CVV number using Regular Expression | Java program to validate CVV ( Card Verification Value ) number using regex . ; Function to validate CVV ( Card Verification Value ) number . using regular expression . ; Regex to check valid CVV number . ; Compile the ReGex ; If the string is empty return false ; Find match between given string and regular expression uaing Pattern . matcher ( ) ; Return if the string matched the ReGex ; Driver code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
import java . util . regex . * ; class GFG { public static boolean isValidCVVNumber ( String str ) { String regex = " ^ [ 0-9 ] { 3,4 } $ " ; 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 = "561" ; System . out . println ( isValidCVVNumber ( str1 ) ) ; String str2 = "5061" ; System . out . println ( isValidCVVNumber ( str2 ) ) ; String str3 = "50614" ; System . out . println ( isValidCVVNumber ( str3 ) ) ; String str4 = "5a # 1" ; System . out . println ( isValidCVVNumber ( str4 ) ) ; } }