473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Validating a string?

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
Nov 15 '05 #1
8 10900
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();
}
}
}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #2
In article <uZ**************@TK2MSFTNGP12.phx.gbl>, sfajeff2
@hotmail.NOSPAM.com says...
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
Nov 15 '05 #3
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.

"Jeff Griffin" <sf******@hotmail.NOSPAM.com> a écrit dans le message de
news:uZ**************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #4
In article <Oa**************@TK2MSFTNGP09.phx.gbl>, fr****@acadx.com
says...
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
Nov 15 '05 #5
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
Nov 15 '05 #6
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.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #7
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
Nov 15 '05 #8
For a good book on Regular Expressions, take a look at "Mastering Regular
Expressions" by Jeffrey Freidl.
David

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brian Kedersha | last post by:
I found code for validating XML documents with the XML Schema cashed for rapid access. Example 3: Validating with XMLSchemaCache. ...
1
by: Christian | last post by:
Hi, I load an Xml-file "customers.xml" into a DataSet (works fine) but then how do I validate it against a schema (e.g. customers.xsd) ? my customers.xml: <?xml version="1.0"...
6
by: Robert Reineri | last post by:
Hello, New to the XML world and .NET. I have what I believe to be a simple problem, but I have read the .NET docs till I'm blue in the face and still can't locate a simple example of how to...
1
by: Andy | last post by:
I am having some trouble validating XML using the XmlValidatingReader. I have created some xml and used the visual studio to generate the schema. So I am confident that the xml and schema match. ...
2
by: Joris Janssens | last post by:
I'm trying to write a program for validating XHTML 1.1-documents against the XHTML 1.1 DTD (which is actually the same as validating an XML-file) but I always get a "(404) Not found" error. This...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating...
5
by: ameen.abdullah | last post by:
Hi Guys, I have a textbox in windows form that should only accept alphabets, numbers, spaces and underscore. If the textbox contains anyother character it should display a msg at the time of...
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions...
6
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.