How to validate CVV number using Regular Expression | Python3 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 ; Return if the string matched the ReGex ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : str4 = "5a1"
import re NEW_LINE def isValidCVVNumber ( str ) : NEW_LINE INDENT regex = " ^ [ 0-9 ] { 3,4 } $ " NEW_LINE p = re . compile ( regex ) NEW_LINE if ( str == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( re . search ( p , str ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT str1 = "561" NEW_LINE print ( isValidCVVNumber ( str1 ) ) NEW_LINE str2 = "5061" NEW_LINE print ( isValidCVVNumber ( str2 ) ) NEW_LINE str3 = "50614" NEW_LINE print ( isValidCVVNumber ( str3 ) ) NEW_LINE print ( isValidCVVNumber ( str4 ) ) NEW_LINE