0

I have the following view model in asp.net mvc app.

[Required]
public string Name { get; set; }
[Required]
public int Age { get; set; }
public DateTime DateOfBirth { get; set; }
public Address CurrentAddress { get; set; }

My Address object contains Post Code property, that has RegularExpession attribute to validate UK post codes.

public class Address
{
   ...
   [RegularExpression(@"^[A-Z]{1,2}[0-9][0-9A-Z]? [0-9][A-Z]{2}$")]
   public string PostCode { get; set; }
   ...
}

I want to expand the current functionality to validate PostCode using different regular expression when for example person is non-Uk resident.

Any ideas how I could achieve that? Is there any way to modify regular expression value at run-time? If you need more information, please let me know and I'll update the question.

Grentley
  • 315
  • 3
  • 6
  • 19
  • Possible duplicate of [How to create a custom validation attribute?](http://stackoverflow.com/questions/11959431/how-to-create-a-custom-validation-attribute) – eocron Feb 06 '17 at 14:54
  • You cannot change the value of the attribute at run-time (attributes are metadata and must be known at compile time). You could write your own conditional validation attribute and pass a regex for each country and conditionally apply the regex based on the selected Country. But your code will be enormous (for example the regex for validating an Australian post code is `^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$`) –  Feb 06 '17 at 23:07
  • And you might want to check [these answers](http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive) that discuss a regex for UK postcodes –  Feb 06 '17 at 23:11

1 Answers1

1

You can create your own Person dependand attribute:

public class MyTestAttribute : ValidationAttribute
{
    private readonly Regex _regex1;
    private readonly Regex _regex2;

    public MyTestAttribute(string regex1, string regex2)
    {
        _regex1 = new Regex(regex1);
        _regex2 = new Regex(regex2);
    }

    public override bool Match(object obj)
    {
        var input = (string) obj;
        if (IsUk())
        {
            return _regex1.IsMatch(input);
        }
        return _regex2.IsMatch(input);
    }

    private bool IsUk()
    {
        //is person in UK
    }
}
eocron
  • 6,885
  • 1
  • 21
  • 50
  • Ok, but how would I pass some parameters in order to specify what regex I'd like to use? – Grentley Feb 06 '17 at 14:40
  • 1
    It might be worth putting in some code to demonstrate how custom Validation Attributes work. Such as how to get the value being validated and how to access a different field on the form which tells you which validation to use. As it is this answer points in the vague direction but isn't really much more than a comment. Certainly not a good answer as is. – Chris Feb 06 '17 at 14:40
  • Actually the reason why I use Address class is because it is heavily reused by other parts of the system. I need to have it working in the same way. – Grentley Feb 06 '17 at 14:46
  • If you share contract - you share API library or wsdl contract. If you bother about sharing library - you have no other way than to update your clients. – eocron Feb 06 '17 at 14:49
  • 1
    @eocron: This is better but the `IsUk` method feels like it is really the most important part of the question. I am assuming that the object has a field called `Country` which would be used to determine whether it is the UK or not. How would you access this field from the validator? – Chris Feb 06 '17 at 14:49
  • Yes, that's a good question. I do have Country property that I would need to pass in to check what regex I'd like to apply to post code field. – Grentley Feb 06 '17 at 14:52
  • You can see full answer here, actually - http://stackoverflow.com/questions/11959431/how-to-create-a-custom-validation-attribute – eocron Feb 06 '17 at 14:52
  • The question I have now is whether jquery validation will pick up the correct regex to validate the field on entering value or will it only work on checking if modelstate is valid? – Grentley Feb 06 '17 at 15:24
  • When I try to pass in Country property to the custom attribute I keep getting "An attribute argument must be a constant expression" error ? – Grentley Feb 06 '17 at 16:17