ValidationAttribute.IsValid Method

Determines whether the specified object is valid.

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

protected virtual ValidationResult IsValid(
	Object value,
	ValidationContext validationContext


Type: System.Object
The object to validate.
Type: System.ComponentModel.DataAnnotations.ValidationContext
An object that contains information about the validation request.

Return Value

Type: System.ComponentModel.DataAnnotations.ValidationResult
true if value is valid; otherwise, false.


The IsValid method is not overridden in the derived class.

You override the IsValid method to provide the logic for your customized validation check. The GetValidationResult and Validate methods call the IsValid method to determine if the value is valid.

The following example shows a customized validation attribute that derives from ValidationAttribute. The class overrides the IsValid method to test whether the value is valid. By default, the attribute displays an error message that is included as a resource in the application.

public class EvenNumberAttribute : ValidationAttribute
    public EvenNumberAttribute() : base(() => Resource1.EvenNumberError) { }
    public EvenNumberAttribute(string errorMessage) : base(() => errorMessage) { }

    protected override ValidationResult IsValid(object value, 
        ValidationContext validationContext)
        if (value == null)
            return ValidationResult.Success;

        int convertedValue;
            convertedValue = Convert.ToInt32(value);
        catch (FormatException)
            return new ValidationResult(Resource1.ConversionError);

        if (convertedValue % 2 == 0)
            return ValidationResult.Success;
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions