Open In App

How to check Aadhaar number is valid or not using Regular Expression

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. It should have 12 digits.
  2. It should not start with 0 and 1.
  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 Aadhaar number.
Input: str = “3675 9834 6012 8” 
Output: false 
Explanation: 
The given string contains 13 digits. Therefore, it is not a valid Aadhaar 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 Aadhaar 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:
 

C++




// C++ program to validate the
// Aadhaar number using Regular Expression
#include <iostream>
#include <regex>
using namespace std;
 
// Function to validate the Aadhaar number.
bool isValidAadhaarNumber(string str)
{
 
  // Regex to check valid Aadhaar number.
  const regex pattern("^[2-9]{1}[0-9]{3}\\s[0-9]{4}\\s[0-9]{4}$");
 
  // If the Aadhaar number
  // is empty return false
  if (str.empty())
  {
     return false;
  }
 
  // Return true if the Aadhaar number
  // matched the ReGex
  if(regex_match(str, pattern))
  {
    return true;
  }
  else
  {
    return false;
  }
}
 
// Driver Code
int main()
{
  // Test Case 1:
  string str1 = "3675 9834 6015";
  cout << isValidAadhaarNumber(str1) << endl;
 
  // Test Case 2:
  string str2 = "4675 9834 6012 8";
  cout << isValidAadhaarNumber(str2) << endl;
 
  // Test Case 3:
  string str3 = "0175 9834 6012";
  cout << isValidAadhaarNumber(str3) << endl;
 
  // Test Case 4:
  string str4 = "3675 98AF 60#2";
  cout << isValidAadhaarNumber(str4) << endl;
 
  // Test Case 5:
  string str5 = "417598346012";
  cout << isValidAadhaarNumber(str5) << endl;
  return 0;
}
 
// This code is contributed by yuvraj_chandra


Java




// Java program to check valid
// Aadhaar number using regex.
 
import java.util.regex.*;
class GFG {
 
    // Function to validate Aadhaar number.
    public static boolean
    isValidAadhaarNumber(String str)
    {
        // Regex to check valid Aadhaar 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(isValidAadhaarNumber(str1));
 
        // Test Case 2:
        String str2 = "4675 9834 6012 8";
        System.out.println(isValidAadhaarNumber(str2));
 
        // Test Case 3:
        String str3 = "0175 9834 6012";
        System.out.println(isValidAadhaarNumber(str3));
 
        // Test Case 4:
        String str4 = "3675 98AF 60#2";
        System.out.println(isValidAadhaarNumber(str4));
 
        // Test Case 5:
        String str5 = "417598346012";
        System.out.println(isValidAadhaarNumber(str5));
    }
}


Python3




# Python3 program to validate
# Aadhaar number using regex.
import re
 
# Function to validate Aadhaar
# number.
def isValidAadhaarNumber(str):
 
    # Regex to check valid
    # Aadhaar 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(isValidAadhaarNumber(str1))
 
# Test Case 2:
str2 = "4675 9834 6012 8"
print(isValidAadhaarNumber(str2))
 
# Test Case 3:
str3 = "0175 9834 6012"
print(isValidAadhaarNumber(str3))
 
# Test Case 4:
str4 = "3675 98AF 60#2"
print(isValidAadhaarNumber(str4))
 
# Test Case 5:
str5 = "417598346012"
print(isValidAadhaarNumber(str5))
 
# This code is contributed by avanitrachhadiya2155


C#




// C# program to validate Aadhaar
// Number using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG
{
   
    // Main Method
    static void Main(string[] args)
    {
       
        // Input strings to Match
        // Aadhaar number
        string[] str={"3675 9834 6015","4675 9834 6012 8" ,"0175 9834 6012","3675 98AF 60#2","417598346012"};
        foreach(string s in str) {
 Console.WriteLine( isValidAadhaarNumber(s) ? "true" : "false");
 }
 Console.ReadKey(); }
    
    // method containing the regex
    public static bool isValidAadhaarNumber(string str)
    {
        string strRegex = @"^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$";
        Regex re = new Regex(strRegex);
        if (re.IsMatch(str))
            return (true);
        else
            return (false);
    }
}
//This code is contributed by Rahul Chauhan


Javascript




// Javascript program to check
// valid Aadhaar number
 
// Function to validate the
// Aadhaar Number 
function isValid_Aadhaar_Number(aadhaar_number)
{
 
    // Regex to check valid
    // aadhaar_number 
    let regex = new RegExp(/^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$/);
 
    // if aadhaar_number
    // is empty return false
    if (aadhaar_number == null) {
        return "false";
    }
 
    // Return true if the aadhaar_number
    // matched the ReGex
    if (regex.test(aadhaar_number) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "3675 9834 6015";
console.log(isValid_Aadhaar_Number(str1));
 
// Test Case 2:
let str2 = "4675 9834 6012 8";
console.log(isValid_Aadhaar_Number(str2));
 
// Test Case 3:
let str3 = "7952 7614 1301";
console.log(isValid_Aadhaar_Number(str3));
 
// Test Case 4:
let str4 = "0175 9834 6012";
console.log(isValid_Aadhaar_Number(str4));
 
// Test Case 5:
let str5 = "3675 98AF 60#2";
console.log(isValid_Aadhaar_Number(str5));
 
// This code is contributed by Rahul Chauhan


Output: 

true
false
false
false
false

 

Time complexity : O(1) as the regex_match function has a constant time complexity. 

Space complexity :O(1) as it only uses a few variables and the regex pattern is pre-compiled and stored in a constant variable.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads