Validates the zip code with regular expression : Zip Code « Regular Expressions « C# / C Sharp






Validates the zip code with regular expression

 

//Microsoft Public License (Ms-PL)
//http://c4fdevkit.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace C4F.DevKit.WebServices
{
    /// <summary>
    /// Provides useful methods like conversion methods.
    /// </summary>
    public static class Utility
    {

        /// <summary>
        /// Validates the zip code
        /// </summary>
        /// <param name="zipCode">Zip code</param>
        /// <returns>True if zip code is valid</returns>
        public static bool ValidateZipCode(string zipCode)
        {
            if (String.IsNullOrEmpty(zipCode))
                return false;

            return Regex.IsMatch(zipCode, "(^[0-9]{5}$)|(^[0-9]{5}-[0-9]{4}$)");
        }
       
    }
}

   
  








Related examples in the same category