How to use a custom validator in with will take in two parameter fields?

Topics: Validation Application Block
Jul 29, 2008 at 9:43 PM
Edited Jul 29, 2008 at 10:38 PM

I am trying to create a custom validator,  using the VAB 3.1, to validate credit card info which will take in card number and type as parameters. In an ASPX page, I will need to get the card type and number.  What can be a good approach for me to validate the card number and the type togetther instead of validate the individual fields separately?    I am getting exceptions at line 60 in ValidationIntegrationHelper.cs  with error message:  {"The property name required to retrieve validation information for integration is invalid or does not belong to a public property."}.  Not sure why that error appears? 

 I would appreciate if you would provide examples of this type.

Thanks very much for the help in advance,

Code snippet:
in the aspx page: I have 




void submitButton_Click(object sender, EventArgs e)
CreditCardValidator CCV = new CreditCardValidator(CardNumberTextBox.Text, CreditCardValidator.CardType.Amex);
validationResultsLabel.Text = CCV.ValidateCreditCard();

and in the CreditCardValidator.cs, I have

CreditCardValidator(string cardNumber, CardType cardType): this(cardNumber, cardType, null, null) {}

with methods: 




override void DoValidate(string objectToValidate, object currentTarget, string key, Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResults validationResults) { ...}
protected override string DefaultMessageTemplate { get{...}}  ...

and an attribute class:

class CreditCardValidatorAttribute : ValidatorAttribute{

private string _cardNumber = string.Empty;

private CreditCardValidator.CardType _cardType;

public CreditCardValidatorAttribute(string cardNumber, CreditCardValidator.CardType cardType)

{  _cardNumber = cardNumber; _cardType = cardType; }

protected override Validator DoCreateValidator(Type targetType)

{ return new CreditCardValidator(_cardNumber, _cardType); }


Jul 30, 2008 at 2:29 PM


This is not how validation integration is used in general. Your code is attaching an event handler and invoking specific code that performs the validation logic, which could be any piece of code (ie it doesn't need to be a validator in the VAB sense).

Typically the validation block is used when you need to decouple the actual validation logic from the code, specifying validation in attributes or configuration instead of invoking the validation logic directly. When used with ASP.NET, there's an extra level of decoupling: validation specified for a type's property is applied to the value in an ASP.NET control. To do this, a PropertyProxyValidator for the field is configured with the type and property names.

From what I can see in your code sample, the credit card type is fixed and what varies is the credit card number. In that case the credit card type would be a piece of configuration for the validator (like the bounds for a string length validator, or the regex for a Regex validator) and the credit card number would be the value to validate.


Jul 30, 2008 at 7:30 PM
Hi Fernando,

Thanks for the suggestions. Yes, the credit card types are fixed, as we only accept certain types of credit cards. However, the credit card number need to be validated against card type, for example Visa and Amex have different card length, and starting digits, besides that the card number needs to conform with Luhn's formula .  So in this types of complex validation scenarios, would it be best not to use VAB, and invoke the validation logic directly instead?

Thanks much for the help,
Jul 30, 2008 at 8:32 PM
Hi Junlia,

I'm not suggesting that you avoid using the VAB; I'm just saying that if you will write code in a click handler to execute an arbitrary piece of code to perform a validation there is no point in making it part of the Validator hierarchy (which would make sense if you want to invoke it through the standard Validator API) or in having a validator instance that owns the value to validate.

A validator should have a configuration that describes how the validation should be performed (whatever it means for the validator) but the validation logic should be performed on the value supplied to the Validator.Validate() method. Whether your CC validator should be configured with the credit card type or this type should be retrieved from another property in the object to validate depends on how you intend to use it; the latter is most likely, unless you keep separate rule sets for different credit card types.

Jul 30, 2008 at 10:24 PM
Hi Fernando,

Thanks for the clarification on when/where to use VAB or not. 

I think standard API suites my needs better as VAB3.1 seems to be a bit overengineered to be extended easily.  I can write a validation for the CC info in an hour, but I have struggled with VAB for a day now, and still can not get it to execute my DoValidate method... Either way, we would process a transaction, and need to validate her inputs  only when the user decide to proceed.

Thanks Much,
Jul 30, 2008 at 10:58 PM
Hi Junlia,

Regardless of whether you use the VAB or not, you may still want to hook your validation to the standard ASP.NET validation step. You can do it by creating your own ASP.NET validator (which is what the VAB ASP.NET integration is about) or set up a validation callback with the standard CustomValidator control.