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

Last Reply 4 months ago By pandeyism

Posted 4 months 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 

You are viewing reply posted by: pandeyism 4 months ago.
Posted 4 months 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