Open In App

Validating Voter ID Card using Regular Expression

Improve
Improve
Like Article
Like
Save
Share
Report

Voter ID card is also known as EPIC (Electors Photo Identity Card). EPIC is an identification proof for Indian citizens over the age of 18.
Given some Voter ids, the task is to check if they are valid or not using regular expressions.

Rules for the valid VOTER ID: 

  • EPIC Number is a unique alphanumeric code. combination of alphabets ( A to Z)and digits (0 to 9).
  • Its length should equal to 10.
  • EPIC Number starts with an alphabet and ends with a digit.
  • EPIC number does not contain whitespaces and other special characters.
  • EPIC number allows only Uppercase letters.

Examples:

Input: XGN3002623 
Output: True

Input: XGS1234567
Output: True

Approach: The problem can be solved using a regular expression based on the following idea:

Create a regex pattern to validate the number as written below:   
regex= “^[A-Z]{3}[0-9]{7}$”  OR Regex=”^[A-Z]{3}\d{7}$”

Where,

  • ^ : This indicates the start of the string
  • [A-Z]{3} : This pattern will match 3 of the preceding items  in the character range “A” to “Z”.
  • [0-9]{7} : This expression will evaluate 7 of the preceding items in the range 0 to 9
  • \d (Digit):  It will match any digit character.
  • {7} Quantifier : match 7 of the preceding item.
  • $ :End of the string.

Follow the below steps to implement the idea:

  • Create a regex expression for Voter ID.
  • Use Pattern class to compile the regex formed.
  • Use the matcher function to check whether the VOTER is valid or not.
  • If it is valid, return true. Otherwise, return false.

Below is the implementation of the above approach:

C++




// C++ program to validate the
// EPIC Number using Regular Expression
 
#include <bits/stdc++.h>
#include <regex>
using namespace std;
 
// Function to validate the EPIC Number
string isValidEPICNumber(string str)
{
    // Regex to check valid EPIC Number.
    const regex pattern("^[A-Z]{3}[0-9]{7}$");
 
    // If the str is empty return false
    if (str.empty()) {
        return "false";
    }
 
    // Return true if the str
    // matched the ReGex
    if (regex_match(str, pattern)) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "XGN3002623";
    cout << isValidEPICNumber(str1) << endl;
 
    // Test Case 2:
    string str2 = "XGS1234567";
    cout << isValidEPICNumber(str2) << endl;
 
    // Test Case 3:
    string str3 = "BNZ1207199";
    cout << isValidEPICNumber(str3) << endl;
 
    // Test Case 4:
    string str4 = "934517865";
    cout << isValidEPICNumber(str4) << endl;
 
    // Test Case 5:
    string str5 = "12071998RAH";
    cout << isValidEPICNumber(str5) << endl;
 
    // Test Case 6:
    string str6 = "654294563";
    cout << isValidEPICNumber(str6) << endl;
 
    return 0;
}


Java




// Java program to validate the
// EPIC Number using Regular Expression
 
import java.util.regex.*;
import java.io.*;
 
class GFG {
 
    // Function to validate the
    // EPIC Number(For India Country Only)
    public static boolean isValidEPICNumber(String str)
    {
        // Regex to check valid EPIC Number
        String regex = "^[A-Z]{3}[0-9]{7}$";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the str is empty return false
        if (str == null) {
            return false;
        }
 
        // Pattern class contains matcher() method
        // to find matching between given
        // EPIC Number using regular expression.
        Matcher m = p.matcher(str);
 
        // Return if the str
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
        // Test Case 1:
        String str1 = "XGN3002623";
        System.out.println(isValidEPICNumber(str1));
 
        // Test Case 2:
        String str2 = "XGS1234567";
        System.out.println(isValidEPICNumber(str2));
 
        // Test Case 3:
        String str3 = "BNZ1207199";
        System.out.println(isValidEPICNumber(str3));
 
        // Test Case 4:
        String str4 = "934517865";
        System.out.println(isValidEPICNumber(str4));
 
        // Test Case 5:
        String str5 = "12071998RAH";
        System.out.println(isValidEPICNumber(str5));
 
        // Test Case 6:
        String str6 = "654294563";
        System.out.println(isValidEPICNumber(str6));
    }
}


Python3




# Python3 program to validate
# EPIC Number using Regular Expression
 
import re
 
# Function to validate
# EPIC Number(For India Country Only)
def isValidEPICNumber(str):
 
    # Regex to check valid EPIC Number
    regex = "^[A-Z]{3}[0-9]{7}$"
     
    # 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
if __name__ == '__main__':
     
    # Test Case 1:
    str1 = "XGN3002623"
    print(isValidEPICNumber(str1))
     
    # Test Case 2:
    str2 = "XGS1234567"
    print(isValidEPICNumber(str2))
     
    # Test Case 3:
    str3 = "BNZ1207199"
    print(isValidEPICNumber(str3))
     
    # Test Case 4:
    str4 = "934517865"
    print(isValidEPICNumber(str4))
     
    # Test Case 5:
    str5 = "12071998RAH"
    print(isValidEPICNumber(str5))
     
    # Test Case 6:
    str6 = "654294563"
    print(isValidEPICNumber(str6))


C#




// C# program to validate the
// EPIC Number using Regular Expression
using System;
using System.Text.RegularExpressions;
 
public class GFG {
    // Function to validate the
    // EPIC Number(For India Country Only)
    public static bool isValidEPICNumber(string str)
    {
        // Regex to check valid EPIC Number
        string regex = "^[A-Z]{3}[0-9]{7}$";
 
        // Compile the ReGex
        Regex p = new Regex(regex);
 
        // If the str is empty return false
        if (str == null) {
            return false;
        }
 
        // Pattern class contains matcher() method
        // to find matching between given
        // EPIC Number using regular expression.
        Match m = p.Match(str);
 
        // Return if the str
        // matched the ReGex
        return m.Success;
    }
 
    // Driver Code.
    public static void Main()
    {
        // Test Case 1:
        string str1 = "XGN3002623";
        Console.WriteLine(isValidEPICNumber(str1));
 
        // Test Case 2:
        string str2 = "XGS1234567";
        Console.WriteLine(isValidEPICNumber(str2));
 
        // Test Case 3:
        string str3 = "BNZ1207199";
        Console.WriteLine(isValidEPICNumber(str3));
 
        // Test Case 4:
        string str4 = "934517865";
        Console.WriteLine(isValidEPICNumber(str4));
 
        // Test Case 5:
        string str5 = "12071998RAH";
        Console.WriteLine(isValidEPICNumber(str5));
 
        // Test Case 6:
        string str6 = "654294563";
        Console.WriteLine(isValidEPICNumber(str6));
    }
}
 
// This code is contributed by Tapesh(tapeshdua420)


Javascript




// Javascript program to validate
// EPIC Number using Regular Expression
 
// Function to validate the
// EPIC Number
function isValidEPICNumber(str) {
    // Regex to check valid
    // EPIC Number
    let regex = new RegExp(/^[A-Z]{3}[0-9]{7}$/);
 
    // if str
    // is empty return false
    if (str == null) {
        return "false";
    }
 
    // Return true if the str
    // matched the ReGex
    if (regex.test(str) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "XGN3002623";
console.log(isValidEPICNumber(str1));
 
// Test Case 2:
let str2 = "XGS1234567";
console.log(isValidEPICNumber(str2));
 
// Test Case 3:
let str3 = "BNZ1207199";
console.log(isValidEPICNumber(str3));
 
// Test Case 4:
let str4 = "934517865";
console.log(isValidEPICNumber(str4));
 
// Test Case 5:
let str5 = "12071998RAH";
console.log(isValidEPICNumber(str5));
 
// Test Case 6:
let str6 = "654294563";
console.log(isValidEPICNumber(str6));


Output

true
true
true
false
false
false

Time Complexity: O(N) for each test case, where N is the length of the given string. 
Auxiliary Space: O(1)  

Related Articles:



Last Updated : 15 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads