Open In App

How to Validate MICR Code using Regular Expression?

Last Updated : 16 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

MICR stands for Magnetic Ink Character Recognition. This technology provides transaction security, ensuring the correctness of bank cheques. MICR code makes cheque processing faster and safer. MICR Technology reduces cheque-related fraudulent activities. Structure of a Magnetic Ink Character Recognition(MICR) Code:

  1. It is a 9-digit code.
  2. It should be in numeric form only.
  3. It should not contain any special characters

MICR comprises 3 parts:

  1. 1st Three digits specify the city code.
  2. The next three digits specify the Bank Code.
  3. The last three indicate the branch code

Note: Similarly, to validate IFSC Code using Regular Expression please refer to this article How to validate IFSC Code using Regular Expression

Examples of Correct MICR Codes

Input: str=”BNZAA2318J”
Output: false
Explanation: As it contains alphabets and the length is not equal to 9.
Input: str1=”123@3459″
Output: False
Explanation: It has a unique character that is against the property of the MICR code.

Input: str2=”9345268″
Output: false
Explanation: As its length is not equal to 9

Input: str3=”934517865″
Output: true
Explanation:

Where,
934 – Indicates the city code 
517 – Indicates the Bank Code
865 – Indicates the Branch Code

Approach

This problem can be dealt with Regular Expression. 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 MICR code As written below:   
regex="^[0-9]{1,9}$"
  • Where,
    • ^: Beginning of the string
    • [0-9]: match any character in the set
    • {1,9}: match Between 1 to 9 of the preceding token
    • $: End of the string

Below is the implementation of the above approach: 

C++




// C++ program to validate the
// MICR Code using Regular
// Expression
 
#include <iostream>
#include <regex>
using namespace std;
 
// Function to validate the
// MICR Code
bool isValidMICRCode(string mICRCode)
{
 
    // Regex to check valid
    // MICR Code.
    const regex pattern("^[0-9]{1,9}$");
 
    // If the MICR Code
    // is empty return false
    if (mICRCode.empty()) {
        return false;
    }
 
    // Return true if the MICR Code
    // matched the ReGex
    if (regex_match(mICRCode, pattern)) {
        return true;
    }
    else {
        return false;
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "BNZAA2318J";
    cout << isValidMICRCode(str1) << endl;
 
    // Test Case 2:
    string str2 = "123@3459";
    cout << isValidMICRCode(str2) << endl;
 
    // Test Case 3:
    string str3 = "BNZAA2318JM";
    cout << isValidMICRCode(str3) << endl;
 
    // Test Case 4:
    string str4 = "934517865";
    cout << isValidMICRCode(str4) << endl;
 
    // Test Case 5:
    string str5 = "Rahul 1998";
    cout << isValidMICRCode(str5) << endl;
 
    // Test Case 6:
    string str6 = "654294563";
    cout << isValidMICRCode(str6) << endl;
 
    return 0;
}


Java




// Java program to validate the
// MICR Code  using Regular Expression
 
import java.util.regex.*;
 
class GFG
{
 
   // Function to validate the
   // MICR Code(For India Country Only)
   public static boolean isValidMICRCode(String MICRCode)
   {
 
       // Regex to check valid MICR Code
       String regex = "^[0-9]{1,9}$";
 
       // Compile the ReGex
       Pattern p = Pattern.compile(regex);
 
       // If the MICR Code
       // is empty return false
       if (MICRCode == null)
       {
           return false;
       }
 
       // Pattern class contains matcher() method
       // to find matching between given
       // MICR Code using regular expression.
       Matcher m = p.matcher(MICRCode);
 
       // Return if the MICR Code
       // matched the ReGex
       return m.matches();
   }
 
   // Driver Code.
   public static void main(String args[])
   {
 
       // Test Case 1:
       String str1 = "BNZAA2318J";
       System.out.println(isValidMICRCode(str1));
 
       // Test Case 2:
       String str2 = "123@3459";
       System.out.println(isValidMICRCode(str2));
 
       // Test Case 3:
       String str3 = "BNZAA2318JM";
       System.out.println(isValidMICRCode(str3));
 
       // Test Case 4:
       String str4 = "934517865";
       System.out.println(isValidMICRCode(str4));
 
       // Test Case 5:
       String str5 = "Rahul 1998";
       System.out.println(isValidMICRCode(str5));
      
       // Test Case 6:
       String str6 = "654294563";
       System.out.println(isValidMICRCode(str6));
 
   }
}


Python




# Python3 program to validate
# MICR Code  using Regular Expression
import re
 
# Function to validate
# MICR Code(For India Country Only)
def isValidMICRCode(str):
 
    # Regex to check valid MICR Code
    regex = "^[0-9]{1,9}$"
     
    # 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 = "BNZAA2318J"
print(isValidMICRCode(str1))
 
# Test Case 2:
str2 = "123@3459"
print(isValidMICRCode(str2))
 
# Test Case 3:
str3 = "Rahul 1998"
print(isValidMICRCode(str3))
 
# Test Case 4:
str4 = "934517865"
print(isValidMICRCode(str4))
 
# Test Case 5:
str5 = "BNZAA2318JM"
print(isValidMICRCode(str5))
 
# Test Case 6:
str6 = "654294563"
print(isValidMICRCode(str6))
 
# This code is contributed by Rahul Chauhan


C#




// C# program to validate the
// MICR Code
//using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG
{
 
  // Main Method
  static void Main(string[] args)
  {
 
    // Input strings to Match
    // MICR Code
    string[] str={"BNZAA2318J","123@3459" ,
                  "BNZAA2318JM","934517865",
                  "Rahul 1998","654294563"};
    foreach(string s in str) {
      Console.WriteLine( isValidMICRCode(s) ? "true" : "false");
    }
    Console.ReadKey(); }
 
  // method containing the regex
  public static bool isValidMICRCode(string str)
  {
    string strRegex = @"^[0-9]{1,9}$";
    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 validate
// MICR Code  using Regular Expression
 
// Function to validate the
// MICR Code 
function isValidMICR_CODE(MICR_CODE) {
    // Regex to check valid
    // MICR CODE
    let regex = new RegExp(/^[0-9]{1,9}$/);
 
    // MICR CODE
    // is empty return false
    if (MICR_CODE == null) {
        return "false";
    }
 
    // Return true if the NUMBERPLATE
    // matched the ReGex
    if (regex.test(MICR_CODE) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "UP 50 BY 1998";
console.log(isValidMICR_CODE(str1));
 
// Test Case 2:
let str2 = "MH 05 DL 9023";
console.log(isValidMICR_CODE(str2));
 
// Test Case 3:
let str3 = "BNZAA2318JM";
console.log(isValidMICR_CODE(str3));
 
// Test Case 4:
let str4 = "MH 05 S 9954";
console.log(isValidMICR_CODE(str4));
 
// Test Case 5:
let str5 = "934517865";
console.log(isValidMICR_CODE(str5));
 
// Test Case 6:
let str6 = "MH 05 DL 9023";
console.log(isValidMICR_CODE(str6));
 
// This code is contributed by Rahul Chauhan


Output

false
false
false
true
false
true

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads