Click here to Skip to main content
15,902,893 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My IBAN Format string is " DE55680501010022067234 " , First two characters represents the country code and followed by 20 numbers

My Code is:
C#
var varText = "DE55680501010022067234 ";
Regex reg = new Regex( @"/^DE\d{20}$/");

if (!reg.IsMatch(varText ))
{
   Response.Write("not valid");
}

How to check this in code behind without using Javascript function.
Posted
Updated 21-Jan-16 1:09am
v4
Comments
Tomas Takac 21-Jan-16 7:12am    
If you have googled you might found this: Regex Class (System.Text.RegularExpressions)[^].
vinu paul 21-Jan-16 7:16am    
Already googled,but can't get my appropriate regular expression
Tomas Takac 21-Jan-16 7:22am    
I would remove the slashes at the beginning and at the end of the regex pattern, then it should be ok.
vinu paul 21-Jan-16 7:34am    
After removing the slashes from the begining and at the end,got the solution.
Thank you :)
Tomas Takac 21-Jan-16 7:25am    
Do you want the matching to succeed even if there are space before and after the IBAN and extract the IBAN itself?

1 solution

Two things, one is that you don't need the leading and trailing "/", that's when you're using js. Second if the only text you're testing contains the number you either need to trim it to remove the trailing space ("$" means "ends with" and your text ends with a space, not a number), or drop the "$", whichever you think gives the best solution.

C#
var varText = "DE55680501010022067234 ";
// dropping the $
Regex reg = new Regex(@"^DE\d{20}");

if (!reg.IsMatch(varText))
{
    Response.Write("not valid");
}

// using Trim
reg = new Regex(@"^DE\d{20}$");

if (!reg.IsMatch(varText.Trim()))
{
    Response.Write("not valid");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900