How to check Aadhar number is valid or not using Regular Expression | Python3 program to validate Aadhar number using regex . ; Function to validate Aadhar number . ; Regex to check valid Aadhar 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 = "3675 ▁ 98AF ▁ 602" ; Test Case 5 :
import re NEW_LINE def isValidAadharNumber ( str ) : NEW_LINE INDENT regex = ( " ^ [ 2-9 ] { 1 } [ 0-9 ] { 3 } \\ " + " s [ 0-9 ] { 4 } \\s [ 0-9 ] { 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 = "3675 ▁ 9834 ▁ 6015" NEW_LINE print ( isValidAadharNumber ( str1 ) ) NEW_LINE str2 = "4675 ▁ 9834 ▁ 6012 ▁ 8" NEW_LINE print ( isValidAadharNumber ( str2 ) ) NEW_LINE str3 = "0175 ▁ 9834 ▁ 6012" NEW_LINE print ( isValidAadharNumber ( str3 ) ) NEW_LINE print ( isValidAadharNumber ( str4 ) ) NEW_LINE str5 = "417598346012" NEW_LINE print ( isValidAadharNumber ( str5 ) ) NEW_LINE