How would I go about having multiple textboxes on an MVC 3 form treated as one for the purposes of validation?
It’s a simple phone number field with one textbox for area code, one for prefix and one for the last four digits.
There are really two validation requirements:
1) They’re all required.
2) They must all contain integers.
Now this is simple when doing it for individual fields but how can I create the equivalent of an ASP.NET CustomValidator with MVC so that I can validate all three fields as a whole?
I actually ended up implementing a custom
ValidationAttributeto solve this, using the same type of logic presented inCompareAttributethat allows you to use reflection to evaluate the values of other properties. This allowed me to implement this at the property level instead of the model level and also allows for client side validation via unobtrusive javascript: