Exemple #1
0
    private static bool ValidatePostalCode(
        IRegularExpressionService regularExpressionService,
        IPostalCodeModel model,
        string country
        )
    {
        var regex = regularExpressionService.GetPostalCodeRegex(country);

        return(Regex.IsMatch(model.PostalCode, regex));
    }
Exemple #2
0
    public bool IsValid(IPostalCodeModel model)
    {
        var postalCodeProperty = model.GetType().GetProperty("PostalCode");

        var attribute = postalCodeProperty.GetCustomAttribute(typeof(PostalCodeAttribute)) as PostalCodeAttribute;

        // Model doesn't implement PostalCodeAttribute
        if (attribute == null)
        {
            return(true);
        }

        return(ValidatePostalCode(_regualrExpressionService, model, attribute.Country));
    }
    public bool IsValid(IPostalCodeModel model)
    {
        var postalCodeProperty = model.GetType().GetProperty("PostalCode");

        var attribute = (PostalCodeAttribute)postalCodeProperty.GetCustomAttribute(typeof(PostalCodeAttribute));

        if (attribute is USPostalCodeAttribute)
        {
            return(ValidateUsPostalCode(model));
        }
        else if (attribute is GBPostalCodeAttribute)
        {
            return(ValidateGBPostalCode(model));
        }
        else
        {
            throw new NotSupportedException("This country's postal code is not supported");
        }
    }
    public bool ValidateGBPostalCode(IPostalCodeModel model)
    {
        var regex = _regularExpressionService.GetPostalCodeRegex("en-GB");

        return(Regex.IsMatch(model.PostalCode, regex));
    }