Monday, April 11, 2011

How to verify Canada postal code by regular expression?

        public static bool IsValid (string postcode)
        {
            if (postcode == ""return true;
            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$");
            return r.IsMatch(postcode.ToUpper());
        }

No comments:

Post a Comment