How to Validate MICR Code using Regular Expression?
Last Updated :
16 Dec, 2022
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:
- It is a 9-digit code.
- It should be in numeric form only.
- It should not contain any special characters
MICR comprises 3 parts:
- 1st Three digits specify the city code.
- The next three digits specify the Bank Code.
- 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
Outputfalse
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)
Similar Reads
How to validate IFSC Code using Regular Expression Given string str, the task is to check whether the given string is a valid IFSC (Indian Financial System) Code or not by using Regular Expression. The valid IFSC (Indian Financial System) Code must satisfy the following conditions: It should be 11 characters long.The first four characters should be
8 min read
How to validate MAC address using Regular Expression Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with
6 min read
How to validate CVV number using Regular Expression Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression. The valid CVV (Card Verification Value) number must satisfy the following conditions: It should have 3 or 4 digits.It should have a digit between 0-9.It should not ha
5 min read
How to validate ISIN using Regular Expressions ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must sati
6 min read
How to validate Visa Card number using Regular Expression Given a string str, the task is to check whether the given string is a valid Visa Card number or not by using Regular Expression. The valid Visa Card number must satisfy the following conditions: It should be 13 or 16 digits long, new cards have 16 digits and old cards have 13 digits.It should start
6 min read
How to validate HTML tag using Regular Expression Given string str, the task is to check whether it is a valid HTML tag or not by using Regular Expression.The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<).It should be followed by a double quotes string or single quotes string.It should not allow on
6 min read
How to validate MasterCard number using Regular Expression Given string str, the task is to check whether the given string is a valid Master Card number or not by using Regular Expression. The valid Master Card number must satisfy the following conditions. It should be 16 digits long.It should start with either two digits numbers may range from 51 to 55 or
7 min read
Regular Expressions to Validate ISBN Code Given some ISBN Codes, the task is to check if they are valid or not using regular expressions. Rules for the valid codes are: It is a unique 10 or 13-digit.It may or may not contain a hyphen.It should not contain whitespaces and other special characters.It does not allow alphabet letters. Examples:
5 min read
How to validate an IP address using Regular Expressions in Java Given an IP address, the task is to validate this IP address with the help of Regular Expressions.The IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.Examples: Inpu
3 min read
How to validate a Username using Regular Expressions in Java Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or
3 min read