Is Valid ZIP Code by regex - CSharp System.Text.RegularExpressions

CSharp examples for System.Text.RegularExpressions:Match Zip

Description

Is Valid ZIP Code by regex

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System;//  w  w w  .j a va2  s. c  o m

public class Main{
        public static bool IsValidZIPCode(string zip)
      {
         // Allows 5 digit, 5+4 digit and 9 digit zip codes must be at least two characters long and
         // caps out at 128 (database size)
         string regExPattern = @"^(\d{5}-\d{4}|\d{5}|\d{9})$";
         return MatchString(zip, regExPattern);
      }
}

Related Tutorials