Exemplo n.º 1
0
        private static void AppendChar(StringBuilder sb, char value, PostcodeOptions options)
        {
            if (options.HasFlag(PostcodeOptions.SkipInvalidCharacters) &&
                !PostcodeValidator.IsValidCharacter(value))
            {
                return;
            }

            if (options.HasFlag(PostcodeOptions.ChangeCase))
            {
                value = StringUtils.ToUpper(value);
            }

            if (!StringUtils.IsWhitespace(value))
            {
                sb.Append(value);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines whether the specified postcode is in the correct format
        /// using the specified options.
        /// </summary>
        /// <param name="postcode">The string to validate.</param>
        /// <param name="options">The options to use when validating.</param>
        /// <returns>
        /// true if <c>postcode</c> is in the correct format for a valid
        /// postcode; otherwise, false.
        /// </returns>
        /// <remarks>
        /// This method cannot determine whether a postcode is a known valid
        /// one, but can help to detect values which adhere to common rules
        /// that all valid postcodes follow.
        /// </remarks>
        public bool IsValid(string postcode, PostcodeOptions options)
        {
            if (postcode == null)
            {
                return(false);
            }

            PostcodeValidator validator = options.HasFlag(PostcodeOptions.Strict) ?
                                          new PostcodeStrictValidator(options) :
                                          new PostcodeValidator(options);

            int index = StringUtils.SkipTrailingWhite(postcode);

            index = validator.CheckInwardCode(postcode, index);

            index = StringUtils.SkipWhitespaceBackwards(postcode, index);
            index = validator.CheckOutwardCode(postcode, index);

            return(index == StringUtils.SkipLeadingWhite(postcode));
        }