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++
#include <iostream>
#include <regex>
using namespace std;
bool isValidMICRCode(string mICRCode)
{
const regex pattern( "^[0-9]{1,9}$" );
if (mICRCode.empty()) {
return false ;
}
if (regex_match(mICRCode, pattern)) {
return true ;
}
else {
return false ;
}
}
int main()
{
string str1 = "BNZAA2318J" ;
cout << isValidMICRCode(str1) << endl;
string str2 = "123@3459" ;
cout << isValidMICRCode(str2) << endl;
string str3 = "BNZAA2318JM" ;
cout << isValidMICRCode(str3) << endl;
string str4 = "934517865" ;
cout << isValidMICRCode(str4) << endl;
string str5 = "Rahul 1998" ;
cout << isValidMICRCode(str5) << endl;
string str6 = "654294563" ;
cout << isValidMICRCode(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG
{
public static boolean isValidMICRCode(String MICRCode)
{
String regex = "^[0-9]{1,9}$" ;
Pattern p = Pattern.compile(regex);
if (MICRCode == null )
{
return false ;
}
Matcher m = p.matcher(MICRCode);
return m.matches();
}
public static void main(String args[])
{
String str1 = "BNZAA2318J" ;
System.out.println(isValidMICRCode(str1));
String str2 = "123@3459" ;
System.out.println(isValidMICRCode(str2));
String str3 = "BNZAA2318JM" ;
System.out.println(isValidMICRCode(str3));
String str4 = "934517865" ;
System.out.println(isValidMICRCode(str4));
String str5 = "Rahul 1998" ;
System.out.println(isValidMICRCode(str5));
String str6 = "654294563" ;
System.out.println(isValidMICRCode(str6));
}
}
|
Python
import re
def isValidMICRCode( str ):
regex = "^[0-9]{1,9}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "BNZAA2318J"
print (isValidMICRCode(str1))
str2 = "123@3459"
print (isValidMICRCode(str2))
str3 = "Rahul 1998"
print (isValidMICRCode(str3))
str4 = "934517865"
print (isValidMICRCode(str4))
str5 = "BNZAA2318JM"
print (isValidMICRCode(str5))
str6 = "654294563"
print (isValidMICRCode(str6))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "BNZAA2318J" , "123@3459" ,
"BNZAA2318JM" , "934517865" ,
"Rahul 1998" , "654294563" };
foreach ( string s in str) {
Console.WriteLine( isValidMICRCode(s) ? "true" : "false" );
}
Console.ReadKey(); }
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 );
}
}
|
Javascript
function isValidMICR_CODE(MICR_CODE) {
let regex = new RegExp(/^[0-9]{1,9}$/);
if (MICR_CODE == null ) {
return "false" ;
}
if (regex.test(MICR_CODE) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "UP 50 BY 1998" ;
console.log(isValidMICR_CODE(str1));
let str2 = "MH 05 DL 9023" ;
console.log(isValidMICR_CODE(str2));
let str3 = "BNZAA2318JM" ;
console.log(isValidMICR_CODE(str3));
let str4 = "MH 05 S 9954" ;
console.log(isValidMICR_CODE(str4));
let str5 = "934517865" ;
console.log(isValidMICR_CODE(str5));
let str6 = "MH 05 DL 9023" ;
console.log(isValidMICR_CODE(str6));
|
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
Please Login to comment...