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


Introduction

In this C++ tutorial, we check whether the input Aadhar number is valid using Regular Expression. Regular expression or regex is used for pattern matching in strings or string searching algorithms. Its functionalities are defined in the <regex> header file. It is used with a regex keyword followed by a variable. Indian citizens are issued an Aadhaar number, which is a unique identification number. It is a 12-digit unique number and no two people have a similar AADHAAR number.

Syntax

regex regular_expression_patternname
return regex_match(value, regular_expression_patternname);

In this tutorial to check the validity of an Aadhar number we consider the following rules of the aadhar number −

  • Adhar number should be of 12 digit numbers.

  • After 4 numbers there should be white space between another 4 numbers.

  • Aadhar numbers should not start with 0 or 1, or special characters.

  • Aadhar number should not contain any alphabet or special characters.

  • Aadhar number should have only numbers.

Demonstration 1

Input = String = "1243 5670 0784"
Output = Input number is a valid aadhar number

Explanation

In the above input string. The input string follows all the listed rules of the aadhar card. Hence, it is a valid aadhar number.

Demonstration 2

Input = String = "1265 A093 5438"
Output = The input string is invalid aadhar number

Explanation

In the above input string, there is an alphabet and an Aadhar card number does not have any alphabet. This number is not a valid Aadhar number.

Demonstration 3

Input = String = "0987 2345 6453"
Output = The input number is invalid Aadhar number

Explanation

In the above input string, it starts with 0 and an Aadhar card number does not start with 0. This number is not a valid Aadhar number.

Algorithm

  • Initialize a 12 digit string.

  • Define regular expressions that follow all the rules of a valid aadhar number.

    • The regular expression is like this − ("^[2-9][0-9]{3}\s[0-9]{4}\s[0-9]{4}$")

    • Here − ^ is the start of the string

    • [2-9] is the first number of the string.

    • [0-9]{3} are the next 3 digits of the string.

    • \s represents white space.

    • [0-9]{4} represents the next 4 digits of the string.

    • \s represents white space.

    • [0-9]{4} represents the next 4 digits of the string.

    • $ represents the end of the string.

  • Match the input string with the regular expression.

  • Print the results of string matching.

Example 1

We implemented the problem statement of this tutorial by defining a regular expression that contains all of the rules of an Aadhar number. In this case, the regular expression matches the input string. Used space in the regular expression to represent white space in the Aadhar number. Used conditional statements to print the results.

#include <iostream>
#include <regex>
using namespace std;

//user-defined function for checking the input string
bool isAadhaarValid(const string& aadhaarVar) {
   // Regular expression defining the rules of valid aadhar number
   regex aadharPattern("^[2-9][0-9]{3} [0-9]{4} [0-9]{4}$");

   return regex_match(aadhaarVar, aadharPattern);
}

int main() {
   // initialize the input string 
   string aadhaarVar = "0175 9834 6012";

   //conditional statement calling function 
   if (isAadhaarValid(aadhaarVar)){
      cout << aadhaarVar << "  is Valid Aadhaar number." <<endl;
   } else {
      cout << aadhaarVar <<" is Invalid Aadhaar number." << endl;
   }

   return 0;
}

Output

0175 9834 6012 is Invalid Aadhaar number

Example 2

We implement the task of checking if the input string is a valid Aadhar number or not, through C++. Define a regular expression for the Aadhar number rules. For white space in the Aadhar number, a special symbol "//s" is used. Multiple strings are used to check whether they are valid Aadhar numbers or not.

#include <iostream>
#include <regex>
using namespace std;
 
// user defined function for checking the input string number
bool isAadhaarNumberValid(string strNum) {
 
   // regular expression defining rules for the valid Aadhar number
   const regex stringPattern("^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$");
 
   // When the input string is empty, return false
   if (strNum.empty()){
      return false;
   }
 
   //conditional statement for checking the string matching
   if(regex_match(strNum, stringPattern)){
      return true;
   } else {
      return false;
   }
}
 
//  Code controller
int main(){
   // string number 1
   string strNum1 = "1265 A093 5438";

   //calling function
   cout << isAadhaarNumberValid(strNum1) << endl;
 
   // string number 2
   string strNum2 = "2435 5670 0784";
   cout << isAadhaarNumberValid(strNum2) << endl;
 
   // string number 3
   string strNum3 = "0987 2345 6453";
   cout << isAadhaarNumberValid(strNum3) << endl;
 
   // string number 4
   string strNum4 = "2675 23476 23 0";
   cout << isAadhaarNumberValid(strNum4) << endl;
 
   // string number 5
   string strNum5 = "2134569847";
   cout << isAadhaarNumberValid(strNum5) << endl;
   return 0;
}

Output

0
1
0
0
0

Conclusion

We have reached the end of this tutorial. This C++ tutorial focused on determining whether an input numeric string is a valid Aadhar number or not. To check the validity of the Aadhar number some rules are defined and the number must follow those rules to be a valid Aadhar number. Used regular expressions for matching string patterns by defining a pattern. Regex is also used in string matching algorithms, input validation and many more. Demonstrated the problem statement with some examples to illustrate its meaning and underlying principle.

Updated on: 03-Oct-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements