Common function to validate IFSC code using Regular Expressions in ASP.Net

Last Reply one year ago By pandeyism

Posted one year ago

VS2010- asp.net, Coding - VB.net

Sir, 

In my project I use to take IFSC (Bank Code) from users.  I need to take this information on many forms.  I want to validate IFSC with a master table.  So I think a gobal function is best for it.  

I rembemer in classical vb6 we had modules to do this work. 

Pl. guide how to use global function in ASP.net and if posible pl give an example too. 

 

Thanks 

Posted one year ago Modified on one year ago

Hi bhushan98,

Create a function that will accept input from user and then validate the input value using RegularExpressions.

Check this example which accepts the below condition.

  1. Length should be 11
  2. First 4 characters are alphabets with upper and lower character
  3. Last seven characters are number

If you want to change the condition then change the RegularExpressions pattern.

HTML

<asp:TextBox runat="server" ID="txtIFSCCode" />
<asp:Button Text="Validate" runat="server" OnClick="Validate" />

Code

C#

protected void Validate(object sender, EventArgs e)
{
    bool isValid = ValidateIFSCCode(txtIFSCCode.Text.Trim());
    Response.Write(isValid ? "Valid" : "Invalid");
}

public bool ValidateIFSCCode(string ifscCode)
{
    System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex("^[A-Za-z]{4}[0-9]{7}$");
    return regx.Matches(ifscCode).Count > 0 ? regx.Matches(ifscCode)[0].Success : false;
}

VB.Net

Protected Sub Validate(ByVal sender As Object, ByVal e As EventArgs)
    Dim isValid As Boolean = ValidateIFSCCode(txtIFSCCode.Text.Trim())
    Response.Write(If(isValid, "Valid", "Invalid"))
End Sub

Public Function ValidateIFSCCode(ByVal ifscCode As String) As Boolean
    Dim regx As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^[A-Za-z]{4}[0-9]{7}$")
    Return If(regx.Matches(ifscCode).Count > 0, regx.Matches(ifscCode)(0).Success, False)
End Function

Screenshot


Posted one year ago

Hi bhushan98,

Create a separate class file and write the code to validate the IFSC code. Then call the method in your required page.

Please take reference to the below code and correct your code.

HTML

<div>
    <asp:TextBox runat="server" ID="txtIFSCCode" />
    <asp:Button ID="Button1" Text="Validate" runat="server" OnClick="Validate" />
</div>

Code

CS.aspx.cs

protected void Validate(object sender, EventArgs e)
{
    IFSC ifsc = new IFSC();
    bool isValid = ifsc.ValidateIFSCCode(txtIFSCCode.Text.Trim());
    Response.Write(isValid ? "Valid" : "Invalid");
}

IFSC.cs

public class IFSC
{
    public bool ValidateIFSCCode(string ifscCode)
    {
        System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex("^[A-Za-z]{4}[0-9]{7}$");
        return regx.Matches(ifscCode).Count > 0 ? regx.Matches(ifscCode)[0].Success : false;
    }
}

Screenshot