Sunday, November 17, 2024
Google search engine
HomeLanguagesValidating Bank Account Number Using Regular Expressions

Validating Bank Account Number Using Regular Expressions

A bank account number is a unique number that is assigned to the account holder after opening their account in any specific bank. In technical terms, we can consider the Bank account number as the Primary Key. A bank account number enables us to do debit, credit, and other transactions. As per RBI Guidelines, Bank Account Number has a unique structure. The structure of the Account Number is as follows:

  • Bank Account Number is written only in numeric form.
  • Bank Account number length varies from 9 digits to 18 digits.
  • No Whitespaces are allowed.
  • Special characters are not allowed.
  • It contains numbers from 0 to 9.

Examples:

Input: str = ”635802010014976”
Output: True
Explanation: It matches the correct Bank Account Number.

Input: str = ” UBIN0563587”
Output: False
Explanation: It should not contains any alphabet characters.

Input: str = ”9136812@895_”
Output: False
Explanation: Underscore and special charactersis not allowed.

Input: str = ”1 2071998”
Output: False
Explanation: Whitespaces are not allowed.

Approach to Validate Account Number Using Regular Expressions

The idea is to use Regular Expression to solve this problem. Regex will validate the entered data and will provide the exact format. Below are steps that can be taken for the problem:

  • Accept the string
  • Create a regex pattern to validate the BANK ACCOUNT NUMBER: 

regex=”^[0-9]{9,18}$”   OR regex=”^\d{9,18}$”

Where,

  • ^ :- Beginning of the string.
  • [0-9] :- Match any character in the set.
  • {9,18} :- Match Between 9 to 18 of the preceding token.
  • $ :- End of the string.

Below is the code implementation of the above approach: 

C++




// C++ program to validate the
// BANK ACCOUNT NUMBER using Regular
// Expression
 
#include <iostream>
#include <regex>
using namespace std;
 
// Function to validate the
//BANK ACCOUNT NUMBER(INDIA COUNTRY ONLY)
bool isValid_Bank_Acc_Number(string bank_account_number)
{
 
    // Regex to check valid
    // bank_account_number Code.
    const regex pattern("^[0-9]{9,18}$");
 
    // If the bank_account_number Code
    // is empty return false
    if (bank_account_number.empty()) {
        return false;
    }
 
    // Return true if the bank_account_number Code
    // matched the ReGex
    if (regex_match(bank_account_number, pattern))
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
void print(bool value){
     
    cout<<"Is this account valid: ";
     
    if(value)
        cout<<"True"<<endl;
    else
        cout<<"False"<<endl;
}
 
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "635802010014976";
    print(isValid_Bank_Acc_Number(str1));
 
    // Test Case 2:
    string str2 = "9136812895_";
    print(isValid_Bank_Acc_Number(str2));
 
    // Test Case 3:
    string str3 = "BNZAA2318JM";
    print(isValid_Bank_Acc_Number(str3));
 
    // Test Case 4:
    string str4 = " 934517865";
    print(isValid_Bank_Acc_Number(str4));
 
    // Test Case 5:
    string str5 = "UBIN0563587";
    print(isValid_Bank_Acc_Number(str5));
     
    // Test Case 6:
    string str6 = "654294563";
    print(isValid_Bank_Acc_Number(str6));
 
    return 0;
}


Java




import java.util.regex.*;
 
class GFG {
 
    // Function to validate the
    // BANK ACCOUNT NUMBER Code(For India Country Only)
    public static String
    isValid_Bank_Acc_Number(String bank_account_number)
    {
 
        // Regex to check valid BANK ACCOUNT NUMBER Code
        String regex = "^[0-9]{9,18}$";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the bank_account_number Code
        // is empty return false
        if (bank_account_number == null) {
            return "False";
        }
 
        // Pattern class contains matcher() method
        // to find matching between given
        // bank_account_number Code using regular
        // expression.
        Matcher m = p.matcher(bank_account_number);
 
        // Return if the bank_account_number Code
        // matched the ReGex
        if (m.matches())
            return "True";
 
        return "False";
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "635802010014976";
        System.out.println("Is this account valid: "
                           + isValid_Bank_Acc_Number(str1));
 
        // Test Case 2:
        String str2 = "9136812895_";
        System.out.println("Is this account valid: "
                           + isValid_Bank_Acc_Number(str2));
 
        // Test Case 3:
        String str3 = "BNZAA2318JM";
        System.out.println("Is this account valid: "
                           + isValid_Bank_Acc_Number(str3));
 
        // Test Case 4:
        String str4 = " 934517865";
        System.out.println("Is this account valid: "
                           + isValid_Bank_Acc_Number(str4));
 
        // Test Case 5:
        String str5 = "UBIN0563587";
        System.out.println("Is this account valid: "
                           + isValid_Bank_Acc_Number(str5));
 
        // Test Case 6:
        String str6 = "654294563";
        System.out.println("Is this account valid: "
                           + isValid_Bank_Acc_Number(str6));
    }
}


Python3




import re
 
# Function to validate
# BANK ACCOUNT NUMBER
def isValid_Bank_Acc_Number(str):
 
    # Regex to check valid BANK ACCOUNT
    regex = "^[0-9]{9,18}$"
 
    # 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 = "635802010014976"
print("Is this account valid:",
      isValid_Bank_Acc_Number(str1))
 
# Test Case 2:
str2 = "9136812895_"
print("Is this account valid:",
      isValid_Bank_Acc_Number(str2))
 
# Test Case 3:
str3 = "BNZAA2318JM"
print("Is this account valid:",
      isValid_Bank_Acc_Number(str3))
 
# Test Case 4:
str4 = " 934517865"
print("Is this account valid:",
      isValid_Bank_Acc_Number(str4))
 
# Test Case 5:
str5 = "UBIN0563587"
print("Is this account valid:",
      isValid_Bank_Acc_Number(str5))
 
# Test Case 6:
str6 = "654294563"
print("Is this account valid:",
      isValid_Bank_Acc_Number(str6))


Javascript




// Javascript program to validate
// BANK ACCOUNT NUMBER Code  using Regular Expression
 
// Function to validate the
// BANK ACCOUNT NUMBER Code 
function isValid_Bank_Acc_Number(bank_account_number) {
    // Regex to check valid
    // BANK ACCOUNT NUMBER CODE
    let regex = new RegExp(/^[0-9]{9,18}$/);
 
    // bank_account_number CODE
    // is empty return false
    if (bank_account_number == null) {
        return "false";
    }
 
    // Return true if the bank_account_number
    // matched the ReGex
    if (regex.test(bank_account_number) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "635802010014976";
console.log("Is this account valid:", isValid_Bank_Acc_Number(str1));
 
// Test Case 2:
let str2 = "9136812895_";
console.log("Is this account valid:", isValid_Bank_Acc_Number(str2));
 
// Test Case 3:
let str3 = "BNZAA2318JM";
console.log("Is this account valid:", isValid_Bank_Acc_Number(str3));
 
// Test Case 4:
let str4 = " 934517865";
console.log("Is this account valid:", isValid_Bank_Acc_Number(str4));
 
// Test Case 5:
let str5 = "UBIN0563587";
console.log("Is this account valid:", isValid_Bank_Acc_Number(str5));
 
// Test Case 6:
let str6 = "654294563";
console.log("Is this account valid:", isValid_Bank_Acc_Number(str6));


C#




// C# program to validate the
// BANK ACCOUNT NUMBER Code(For India Country Only)
//using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG
{
 
// Main Method
static void Main(string[] args)
{
 
    // Input strings to Match
    // BANK ACCOUNT NUMBER Code(For India Country Only)
    string[] str={"635802010014976","9136812895_" ,
                "BNZAA2318JM"," 934517865",
                "UBIN0563587","654294563"};
    foreach(string s in str) {
    Console.WriteLine( isValid_Bank_Acc_Number(s) ? "true" : "false");
    }
    Console.ReadKey(); }
 
// method containing the regex
public static bool isValid_Bank_Acc_Number(string str)
{
    string strRegex = @"^[0-9]{9,18}$";
    Regex re = new Regex(strRegex);
    if (re.IsMatch(str))
    return (true);
    else
    return (false);
}
}


Output

Is this account valid: True
Is this account valid: False
Is this account valid: False
Is this account valid: False
Is this account valid: False
Is this account valid: True

Time complexity: O(n) // where n is the length of string.

Auxiliary space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments