In Application_Start add the following line to associate the adapter with your custom attribute, which will be responsible for issuing client-side validation attributes:
DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(SsnAttribute), typeof(RegularExpressionAttributeAdapter) );
The reason you need this is the RegularExpressionAttribute method. It does not implement the IClientValidatable interface, but rather has a RegularExpressionAttributeAdapter associated with it.
In your case, you have a custom attribute that comes from RegularExpressionAttribute , but your attribute does not implement the IClientValidatable interface to check the health of the client and does not have an attribute adapter associated with it (unlike its parent class). Thus, your SsnAttribute should either implement the IClientValidatable interface or bind the adapter, as suggested earlier in my answer.
This, as they say, personally, I donβt see much point in implementing this special verification attribute. In this case, the constant may be sufficient:
public const string Ssn = @"^(?!000)(?!666)(?!9[0-9][0-9])\d{3}[- ]?(?!00)\d{2}[- ]?(?!0000)\d{4}$", ErrorMessage = "The SSN you entered is invalid. If you do not have this number please leave the field blank";
and then:
public class FooModel { [RegularExpression(Ssn, ErrorMessage = "The SSN you entered is invalid. If you do not have this number please leave the field blank")] public string Ssn { get; set; } }
seems quite readable.
source share