# How to check Aadhar number is valid or not using Regular Expression

Given string str, the task is to check whether the given string is a valid Aadhar number or not by using Regular Expression. The valid Aadhar number must satisfy the following conditions:

1. It should have 12 digits.
3. It should not contain any alphabet and special characters.
4. It should have white space after every 4 digits.

Examples:

Input: str = “3675 9834 6012”
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid Aadhar number.
Input: str = “3675 9834 6012 8”
Output: false
Explanation:
The given string contains 13 digits. Therefore, it is not a valid Aadhar number.

Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer:

• Get the String.
• Create a regular expression to check valid Aadhar number as mentioned below:

regex = “^[2-9]{1}[0-9]{3}\\s[0-9]{4}\\s[0-9]{4}\$”;

• Where:
• ^ represents the starting of the string.
• [2-9]{1} represents the first digit should be any from 2-9.
• [0-9]{3} represents the next 3 digits after the first digit should be any digit from 0-9.
• \\s represents white space.
• [0-9]{4} represents the next 4 digits should be any from 0-9.
• \\s represents white space.
• [0-9]{4} represents the next 4 digits should be any from 0-9.
• \$ represents the ending of the string.
• Match the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
• Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:

## Java

 `// Java program to check valid` `// Aadhar number using regex.`   `import` `java.util.regex.*;` `class` `GFG {`   `    ``// Function to validate Aadhar number.` `    ``public` `static` `boolean` `    ``isValidAadharNumber(String str)` `    ``{` `        ``// Regex to check valid Aadhar number.` `        ``String regex` `            ``= ``"^[2-9]{1}[0-9]{3}\\s[0-9]{4}\\s[0-9]{4}\$"``;`   `        ``// Compile the ReGex` `        ``Pattern p = Pattern.compile(regex);`   `        ``// If the string is empty` `        ``// return false` `        ``if` `(str == ``null``) {` `            ``return` `false``;` `        ``}`   `        ``// Pattern class contains matcher() method` `        ``// to find matching between given string` `        ``// and regular expression.` `        ``Matcher m = p.matcher(str);`   `        ``// Return if the string` `        ``// matched the ReGex` `        ``return` `m.matches();` `    ``}`   `    ``// Driver Code.` `    ``public` `static` `void` `main(String args[])` `    ``{`   `        ``// Test Case 1:` `        ``String str1 = ``"3675 9834 6015"``;` `        ``System.out.println(isValidAadharNumber(str1));`   `        ``// Test Case 2:` `        ``String str2 = ``"4675 9834 6012 8"``;` `        ``System.out.println(isValidAadharNumber(str2));`   `        ``// Test Case 3:` `        ``String str3 = ``"0175 9834 6012"``;` `        ``System.out.println(isValidAadharNumber(str3));`   `        ``// Test Case 4:` `        ``String str4 = ``"3675 98AF 60#2"``;` `        ``System.out.println(isValidAadharNumber(str4));`   `        ``// Test Case 5:` `        ``String str5 = ``"417598346012"``;` `        ``System.out.println(isValidAadharNumber(str5));` `    ``}` `}`

## Python3

 `# Python3 program to validate ` `# Aadhar number using regex. ` `import` `re`   `# Function to validate Aadhar` `# number. ` `def` `isValidAadharNumber(``str``):`   `    ``# Regex to check valid ` `    ``# Aadhar number. ` `    ``regex ``=` `(``"^[2-9]{1}[0-9]{3}\\"` `+` `             ``"s[0-9]{4}\\s[0-9]{4}\$"``)` `    `  `    ``# Compile the ReGex` `    ``p ``=` `re.``compile``(regex)`   `    ``# If the string is empty ` `    ``# return false` `    ``if` `(``str` `=``=` `None``):` `        ``return` `False`   `    ``# Return if the string ` `    ``# matched the ReGex` `    ``if``(re.search(p, ``str``)):` `        ``return` `True` `    ``else``:` `        ``return` `False`   `# Driver code`   `# Test Case 1:` `str1 ``=` `"3675 9834 6015"` `print``(isValidAadharNumber(str1))`   `# Test Case 2:` `str2 ``=` `"4675 9834 6012 8"` `print``(isValidAadharNumber(str2))`   `# Test Case 3:` `str3 ``=` `"0175 9834 6012"` `print``(isValidAadharNumber(str3))`   `# Test Case 4:` `str4 ``=` `"3675 98AF 60#2"` `print``(isValidAadharNumber(str4))`   `# Test Case 5:` `str5 ``=` `"417598346012"` `print``(isValidAadharNumber(str5))`   `# This code is contributed by avanitrachhadiya2155`

Output:

```true
false
false
false
false

```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up External Technical Content Reviewer at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.