Validating a string?

  • Thread starter Thread starter Jeff Griffin
  • Start date Start date
J

Jeff Griffin

I am looking for a good referance on how to validate a string in C#.
Specifically have a string which will contain a postal code that may or may
not be in the US. I need to check this string to decide wheather it is in
the US or outside the US. I think all I really need to do is see if the
string meets the form of five integers. If it does then I will assume that
it is a US zipcode and if it does not then I will assume that it is outside
the US.

Anyways sorry for the long explination but all I really need is a good
referance on how to check the string to see if it is 5 integers or somthing
else.

Thanks, Jeff Griffin
 
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.Write("Enter a zip code: ");
string input = Console.ReadLine();
// Be sure to allow for ZIP + 4 as well
Match match = Regex.Match(input, @"^\d{5}(-\d{4})?$");
Console.WriteLine(match.Success);
Console.ReadLine();
}
}
}
 
sfajeff2 said:
I am looking for a good referance on how to validate a string in C#.
Specifically have a string which will contain a postal code that may or may
not be in the US. I need to check this string to decide wheather it is in
the US or outside the US. I think all I really need to do is see if the
string meets the form of five integers. If it does then I will assume that
it is a US zipcode and if it does not then I will assume that it is outside
the US.

Anyways sorry for the long explination but all I really need is a good
referance on how to check the string to see if it is 5 integers or somthing
else.

Thanks, Jeff Griffin
Hi Jeff,
I'll write some code in here, so you may check it for typos.

bool CheckZIP(string sZip)
{
bool bSuccess = false;

if (sZip.Lenght == 5)
{
int i;
try
{
i = Int32.Parse(sZip); //contains only digits
//if we reach here, the string contains only
//digits, else, we'll be in the catch block
bSuccess = true;
}
catch
{
//nothing to do, just avoid exception
//the bSuccess is already false
}
}

return bSuccess;
}

Hope that helps.
Sunny
 
Zip codes are 5 integers in France, and probably in lots of other countries.
So, you should not use this criteria to infer the country. You should add
some UI to let the user select the country (if your app has a UI, of
course).

Otherwise the System.Text.RegularExpression is a good place to start, or you
could write a specialized method that tests the string length and its
contents.

static bool IsZip5(string s)
{
if (s.Length != 5)
return false;
foreach (char ch in s)
{
if (!Char.IsDigit(ch))
return false;
}
return true;
}

Bruno.
 
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.Write("Enter a zip code: ");
string input = Console.ReadLine();
// Be sure to allow for ZIP + 4 as well
Match match = Regex.Match(input, @"^\d{5}(-\d{4})?$");
Console.WriteLine(match.Success);
Console.ReadLine();
}
}
}
Perfect, I have to read more about that regex stuff ...

So, discard my other post :)

Sunny
 
Frank, Sunny, Bruno- Thank yall so much, that was exactly what I needed.

Frank - Do you know of a site (or book) that explains more about the
following and what it all means:

@"^\d{5}(-\d{4})?$");

This seems to work great for what I need, but I would like to learn more
about what all this means. I am in college and have not learned about any of
this yet and would like to learn some more if I ever need to use this kind
of thing again.

Bruno - That is a good point, I was unaware that france used 5 digit
zipcodes as well. I have been mostly dealing with Canada and the UK. If
anyone else reads this and is trying to do somthing similar I was able to
find a solution to this by calling a web service that validates a US zipcode
if it meets th 5 integer criteria. I know I could have just done this to
begin with to check, but since it is a webservice it produces a perforamance
penalty, thus I find it better to do the check for 5 integers first and then
if it meets that criteria I check it aginst the webservice. This negates the
performance penalty for those with a "zipcode" that does not conform to the
5 integer rule.

Again, Thank you all very much!

Jeff Griffin
 
Thus spake Jeff Griffin:
Frank - Do you know of a site (or book) that explains more about the
following and what it all means:

@"^\d{5}(-\d{4})?$");

Do what I did: use Google. ;)
This seems to work great for what I need, but I would like to learn
more about what all this means. I am in college and have not learned
about any of this yet and would like to learn some more if I ever
need to use this kind of thing again.

A search on "c# regex tutorial" yielded dozens of pages on the subject.
 
Just what I needed, Thanks Frank! (I was trying to look up "validate C#
string" which returned some allright results, but a lot of the same article
over and over again, just a different sites).

Jeff Griffin
 
For a good book on Regular Expressions, take a look at "Mastering Regular
Expressions" by Jeffrey Freidl.
David
 
Back
Top