Skip to content
Related Articles
Open in App
Not now

Related Articles

Validating UPI IDs using Regular Expressions

Improve Article
Save Article
  • Last Updated : 14 Dec, 2022
Improve Article
Save Article

Given some UPI IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid UPI ID: 

  • UPI ID is an alphanumeric String i.e., formed using digits(0-9), alphabets (A-Z and a-z), and other special characters.
  • It must contain ‘@’.
  • It should not contain whitespace.
  • It may or may not contain a dot (.) or hyphen (-).

UPI stands for Unified Payments Interface (UPI).UPI IDs are unique IDs given to each customer.

Examples :

Input: str = ”9136812895@ybl″
Output: True

Input: str = ”MH05DL9023 ”
Output: false
Explanation: It does not contain the “@” symbol.

Input: str = ”ViratKohli101@paytm″
Output: true

Input: str =”rahul.12chauhan1998-1@okicici″
Output: true

Input: str = ”1234567890@upi123456″
Output: true

Input: str = ”Akanksha  @ybl ”
Output: false
Explanation: It contains a whitespace.

Approach: The problem can be solved based on the following idea:

Create a regex pattern to validate the number as written below:   
regex=”^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$”

^ : Beginning of the string.
[] Character set :Match any character  in the set.
[a-zA-Z0-9.-] : Match any character in the range a-z, A-Z, 0-9, hyphen(-), dot(.).
{2, 256} Quantifier: Match between 2 and 256 of the preceding items.
$: End of the string

Follow the below steps to implement the idea:

  • Create regex expression for UPI ID.
  • Use Pattern class to compile the regex formed.
  •  Use the matcher function to check whether the UPI id 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
// UPI ID using Regular
// Expression

#include <bits/stdc++.h>
#include <regex>
using namespace std;

// Function to validate the
// upi_Id Code
string isValidUpi(string upi_Id)
{
    // Regex to check valid upi_Id Code
    const regex pattern(
        "^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$");

    // If the upi_Id Code
    // is empty return false
    if (upi_Id.empty()) {
        return "false";
    }

    // Return true if the upi_Id Code
    // matched the ReGex
    if (regex_match(upi_Id, pattern)) {
        return "true";
    }
    else {
        return "false";
    }
}

// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "9136812895@ybl";
    cout << isValidUpi(str1) << endl;

    // Test Case 2:
    string str2 = "rahul.12chauhan1998-1@okicici";
    cout << isValidUpi(str2) << endl;

    // Test Case 3:
    string str3 = "BNZAA2318JM";
    cout << isValidUpi(str3) << endl;

    // Test Case 4:
    string str4 = "934517865";
    cout << isValidUpi(str4) << endl;

    // Test Case 5:
    string str5 = "ViratKohli101@paytm";
    cout << isValidUpi(str5) << endl;

    // Test Case 6:
    string str6 = "Akanksha  @ybl";
    cout << isValidUpi(str6) << endl;

    return 0;
}

Java

// Java program to validate the
// UPI ID using Regular Expression

import java.util.regex.*;

class GFG {
    // Function to validate the
    // UPI ID(For India Country Only)
    public static boolean isValidUpi(String upi_Id)
    {

        // Regex to check valid upi_Id Code
        String regex
            = "^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$";

        // Compile the ReGex
        Pattern p = Pattern.compile(regex);

        // If the upi_Id Code
        // is empty return false
        if (upi_Id == null) {
            return false;
        }

        // Pattern class contains matcher()
        // method to find matching between
        // given MICR Code using regex
        Matcher m = p.matcher(upi_Id);

        // Return if the upi_Id Code
        // matched the ReGex
        return m.matches();
    }

    // Driver Code.
    public static void main(String args[])
    {

        // Test Case 1:
        String str1 = "9136812895@ybl";
        System.out.println(isValidUpi(str1));

        // Test Case 2:
        String str2 = "rahul.12chauhan1998-1@okicici";
        System.out.println(isValidUpi(str2));

        // Test Case 3:
        String str3 = "BNZAA2318JM";
        System.out.println(isValidUpi(str3));

        // Test Case 4:
        String str4 = "934517865";
        System.out.println(isValidUpi(str4));

        // Test Case 5:
        String str5 = "ViratKohli101@paytm";
        System.out.println(isValidUpi(str5));

        // Test Case 6:
        String str6 = "Akanksha  @ybl";
        System.out.println(isValidUpi(str6));
    }
}

Python3

# Python3 program to validate
# UPI ID  using Regular Expression

import re

# Function to validate
# UPI ID(For India Country Only)
def isValidUpi(str):

    # Regex to check valid UPI ID
    regex = "^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$"

    # 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 = "9136812895@ybl"
    print(isValidUpi(str1))
    
    # Test Case 2:
    str2 = "rahul.12chauhan1998-1@okicici"
    print(isValidUpi(str2))
    
    # Test Case 3:
    str3 = "Rahul 1998"
    print(isValidUpi(str3))
    
    # Test Case 4:
    str4 = "934517865"
    print(isValidUpi(str4))
    
    # Test Case 5:
    str5 = "ViratKohli101@paytm"
    print(isValidUpi(str5))
    
    # Test Case 6:
    str6 = "Akanksha  @ybl"
    print(isValidUpi(str6))

C#

// C# program to validate the
// UPI ID using Regular Expression
using System;
using System.Text.RegularExpressions;

public class GFG 
{
  
  // Function to validate the
  // UPI ID(For India Country Only)
  public static bool isValidUpi(string upi_Id)
  {

    // Regex to check valid upi_Id Code
    string regex
      = "^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$";

    // Compile the ReGex
    Regex p = new Regex(regex);

    // If the upi_Id Code
    // is empty return false
    if (upi_Id == null) {
      return false;
    }

    // Pattern class contains matcher()
    // method to find matching between
    // given MICR Code using regex
    Match m = p.Match(upi_Id);

    // Return if the upi_Id Code
    // matched the ReGex
    return m.Success;
  }

  // Driver Code.
  public static void Main()
  {

    // Test Case 1:
    string str1 = "9136812895@ybl";
    Console.WriteLine(isValidUpi(str1));

    // Test Case 2:
    string str2 = "rahul.12chauhan1998-1@okicici";
    Console.WriteLine(isValidUpi(str2));

    // Test Case 3:
    string str3 = "BNZAA2318JM";
    Console.WriteLine(isValidUpi(str3));

    // Test Case 4:
    string str4 = "934517865";
    Console.WriteLine(isValidUpi(str4));

    // Test Case 5:
    string str5 = "ViratKohli101@paytm";
    Console.WriteLine(isValidUpi(str5));

    // Test Case 6:
    string str6 = "Akanksha @ybl";
    Console.WriteLine(isValidUpi(str6));
  }
}

// This code is contributed by Pushpesh Raj.

Javascript

// Javascript program to validate
// UPI ID  using Regular Expression

// Function to validate the
// upi_Id Code  
function isValid_UPI_ID(upi_Id) {
    // Regex to check valid
    // upi_Id
    let regex = new RegExp(/^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$/);

    // upi_Id
    // is empty return false
    if (upi_Id == null) {
        return "false";
    }

    // Return true if the upi_Id
    // matched the ReGex
    if (regex.test(upi_Id) == true) {
        return "true";
    }
    else {
        return "false";
    }
}

// Driver Code
// Test Case 1:
let str1 = "9136812895@ybl";
console.log(isValid_UPI_ID(str1));

// Test Case 2:
let str2 = "rahul.12chauhan1998-1@okicici";
console.log(isValid_UPI_ID(str2));

// Test Case 3:
let str3 = "BNZAA2318JM";
console.log(isValid_UPI_ID(str3));

// Test Case 4:
let str4 = "MH 05 S 9954";
console.log(isValid_UPI_ID(str4));

// Test Case 5:
let str5 = "ViratKohli101@paytm";
console.log(isValid_UPI_ID(str5));

// Test Case 6:
let str6 = "Akanksha  @ybl";
console.log(isValid_UPI_ID(str6));
Output

true
true
false
false
true
false

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!