Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#- Validation for US or Canadian zip code

Tags:

I am using following method to validate US or Canadian zip code, but i think it is not working fine for me. Please suggest me the changes in the regular expression.

private bool IsUSorCanadianZipCode(string zipCode)     {         bool isValidUsOrCanadianZip = false;         string pattern = @"^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$";         Regex regex = new Regex(pattern);         return isValidUsOrCanadianZip = regex.IsMatch(zipCode);     } 

Thanks.

like image 775
Sunil Singh Avatar asked Feb 18 '13 18:02

Sunil Singh


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


1 Answers

    var _usZipRegEx = @"^\d{5}(?:[-\s]\d{4})?$";     var _caZipRegEx = @"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$";      private bool IsUSOrCanadianZipCode(string zipCode)     {         var validZipCode = true;         if ((!Regex.Match(zipCode, _usZipRegEx).Success) && (!Regex.Match(zipCode, _caZipRegEx).Success))         {             validZipCode = false;         }         return validZipCode;     } } 
like image 113
d.moncada Avatar answered Nov 26 '22 07:11

d.moncada