MatchRegex() public static method

Checks if input matches regex.
public static MatchRegex ( Regex, regex, string input ) : bool
regex Regex,
input string
return bool
Example #1
0
        /// <summary>
        /// Regex check for action parameter.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="propName"></param>
        private void CheckRegex(string input, string propName)
        {
            var localizedPattern = _localizer[$"RegexPattern{propName}"];

            if (!RegexMatcher.MatchRegex(input, _localizer[localizedPattern]))
            {
                var exampleFormat = _localizer[$"RegexExample{propName}"];
                throw new MilvaUserFriendlyException("RegexErrorMessage", _localizer[$"Localized{propName}"], exampleFormat);
            }
        }
Example #2
0
    /// <summary>
    /// Determines whether the specified value of the object is valid.
    /// </summary>
    /// <param name="value"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    protected override ValidationResult IsValid(object value, ValidationContext context)
    {
        var sharedLocalizer = context.GetLocalizerInstance(_resourceType);

        var localizedPropName = sharedLocalizer == null ? context.MemberName : sharedLocalizer[$"{LocalizerKeys.Localized}{MemberNameLocalizerKey ?? context.MemberName}"];

        if (sharedLocalizer != null)
        {
            if (IsRequired)
            {
                if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
                {
                    var localizedPattern = sharedLocalizer[$"{LocalizerKeys.RegexPattern}{MemberNameLocalizerKey ?? context.MemberName}"];

                    if (RegexMatcher.MatchRegex(value.ToString(), sharedLocalizer[localizedPattern]))
                    {
                        return(ValidationResult.Success);
                    }
                    else
                    {
                        var exampleFormat = sharedLocalizer[ExampleFormatLocalizerKey ?? LocalizerKeys.RegexExample + context.MemberName];
                        ErrorMessage = sharedLocalizer[LocalizerKeys.RegexErrorMessage, localizedPropName, exampleFormat];
                        return(new ValidationResult(FormatErrorMessage("")));
                    }
                }
                else
                {
                    ErrorMessage = sharedLocalizer[LocalizerKeys.PropertyIsRequired, localizedPropName];
                    return(new ValidationResult(FormatErrorMessage("")));
                }
            }
            else
            {
                if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
                {
                    var localizedPattern = sharedLocalizer[$"{LocalizerKeys.RegexPattern}{MemberNameLocalizerKey ?? context.MemberName}"];

                    if (RegexMatcher.MatchRegex(value.ToString(), sharedLocalizer[localizedPattern]))
                    {
                        return(ValidationResult.Success);
                    }
                    else
                    {
                        var exampleFormat = sharedLocalizer[ExampleFormatLocalizerKey ?? LocalizerKeys.RegexExample + context.MemberName];
                        ErrorMessage = sharedLocalizer[LocalizerKeys.RegexErrorMessage, localizedPropName, exampleFormat];
                        return(new ValidationResult(FormatErrorMessage("")));
                    }
                }
                else
                {
                    return(ValidationResult.Success);
                }
            }
        }
        else
        {
            if (base.IsValid(value))
            {
                return(ValidationResult.Success);
            }
            else
            {
                ErrorMessage = $"{LocalizerKeys.PleaseEnterAValid} {context.MemberName}";
                return(new ValidationResult(FormatErrorMessage("")));
            }
        }
    }