How to check Aadhar number is valid or not using Regular Expression | Java program to check valid 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 ; Pattern class contains matcher ( ) method to find matching between given string and regular expression . ; 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 isValidAadharNumber ( String str ) { String regex = " ^ [ 2-9 ] { 1 } [ 0-9 ] { 3 } \\ s [ 0-9 ] { 4 } \\ s [ 0-9 ] { 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 = "3675 ▁ 9834 ▁ 6015" ; System . out . println ( isValidAadharNumber ( str1 ) ) ; String str2 = "4675 ▁ 9834 ▁ 6012 ▁ 8" ; System . out . println ( isValidAadharNumber ( str2 ) ) ; String str3 = "0175 ▁ 9834 ▁ 6012" ; System . out . println ( isValidAadharNumber ( str3 ) ) ; String str4 = "3675 ▁ 98AF ▁ 60#2" ; System . out . println ( isValidAadharNumber ( str4 ) ) ; String str5 = "417598346012" ; System . out . println ( isValidAadharNumber ( str5 ) ) ; } }