Address and phone validation

  • Thread starter Thread starter Srinivas Kollipara
  • Start date Start date
S

Srinivas Kollipara

Hey guys,
i have a small question in C#. I have a windows form which has Address
fields, zip code and phone number fields. it is working fine with U.S. but i
need to modify that for all the other countries. is there any way i can
validate zipcode and phone numbers.... Thanks in advance.
bye
Srinivas
 
Srinivas,

You should be able to determine the locale of where the app is running and
then choose the correct regular expression to validate the field. I have
posted a couple of links below to help you along the way.

Hope this helps.
 
thanks for quick response. i didnt get much help from the links u sent. what
i need to do is, if some one selects a country suppose 'china' we need to
validate the format of zip code for china. and the same with phone number..
thanks in advance
srinivas

Brian Brown said:
Srinivas,

You should be able to determine the locale of where the app is running and
then choose the correct regular expression to validate the field. I have
posted a couple of links below to help you along the way.

Hope this helps.
http://msdn.microsoft.com/library/default.asp?url=/msdnmag/issues/02/06/internat/toc.asp?frame=true
[/QUOTE]
 
Hey guys,
i have a small question in C#. I have a windows form which has Address
fields, zip code and phone number fields. it is working fine with U.S. but
i
need to modify that for all the other countries. is there any way i can
validate zipcode and phone numbers.... Thanks in advance.

Create a class similar to NumberFormatInfo to contain regular expressions
for validating phone numbers, zip codes and similar. Create one instance for
every single culture your targeting and store the instance in an Hashtable
using the culture as the key. When validating an address retrieve the
correct "address info" object from the hashtable and use the regular
expressions to validate the different fields.

You will have to do some research to gather zip-code and telephone number
validation rules for all of the cultures though...

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Srinivas,

I am sorry that the links that I provided did not help you. So I have
posted a code sample below to expand on the post that provided earlier. To
simplify the example I am using US date format and British date format to
validate against. To execute the example create a console application and
copy and paste the code below. Watch out for any line wrap. If you have any
questions please reply to this post.

I hope this helps!


--------------------------

using System;
using System.Collections;
using System.Globalization;
using System.Text.RegularExpressions;

namespace ResourceRegExpTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
//set up the Current Culture of this thread
string strCulture = CultureInfo.CurrentCulture.Name;
//set up the regular expressions associated with the culture (these are
for date
Hashtable CultRegExp = new Hashtable();
CultRegExp.Add("en-US",@"(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[-
/.](19|20)\d\d"); //English-US (mm/dd/yyyy)
CultRegExp.Add("en-GB",@"(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[-
/.](19|20)\d\d"); //English - United Kingdom (dd/mm/yyyy)

//get a date from the console input
Console.Write("Please enter a date: " );
string strDate = Console.ReadLine();

//get the current culture of the thread
if(CultRegExp.ContainsKey(strCulture))
if(Regex.IsMatch(strDate,CultRegExp[strCulture].ToString()))
Console.WriteLine("The string entered was a date match to the culture:
" + strCulture);
else
Console.WriteLine("The string entered was NOT a match to the culture: "
+ strCulture);
else
Console.WriteLine("There was no match in the hashtable for the culture:
" + strCulture);


}
}
}
 
Back
Top