Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

DXValidationProvider.ValidationMode Property

Gets or sets the validation mode.

Namespace: DevExpress.XtraEditors.DXErrorProvider

Assembly: DevExpress.XtraEditors.v24.2.dll

NuGet Package: DevExpress.Win.Navigation

#Declaration

[DefaultValue(ValidationMode.Default)]
[DXCategory("Behavior")]
public ValidationMode ValidationMode { get; set; }

#Property Value

Type Default Description
DevExpress.XtraEditors.DXErrorProvider.ValidationMode Default

A DevExpress.XtraEditors.DXErrorProvider.ValidationMode enumeration value that specifies the validation mode.

#Remarks

By default, automatic checking is disabled. You should manualy invoke the validation mechanism when required. To do this, use the DXValidationProvider.Validate method.

To enable automatic validation, set the ValidationMode property to Auto. In this case, an editor’s value will be automatically checked against the associated validation rule when data must be validated (for instance, when focus moves to another control).

#Example

This example demonstrates how to validate data via the DXValidationProvider component. In this example, validation rules (built-in and custom) are created in code, and associated with editors using the DXValidationProvider.SetValidationRule method.

The CustomValidationRule class represents a custom validation rule that checks whether the editor’s value begins with “Dr.”, “Mr.”, “Mrs.”, “Miss” or “Ms.”.

using DevExpress.XtraEditors.DXErrorProvider;

// Validates the editors when a user presses/clicks the OK button.
dxValidationProvider1.ValidationMode = ValidationMode.Manual;

// ...
ConditionValidationRule containsValidationRule = new ConditionValidationRule();
containsValidationRule.ConditionOperator = ConditionOperator.Contains;
containsValidationRule.Value1 = '@';
containsValidationRule.ErrorText = "Please enter a valid email";
containsValidationRule.ErrorType = ErrorType.Warning;

CompareAgainstControlValidationRule compValidationRule = 
    new CompareAgainstControlValidationRule();
compValidationRule.Control = notEmptyTextEdit;
compValidationRule.CompareControlOperator = CompareControlOperator.Equals;
compValidationRule.ErrorText = "Please enter a value that equals to the first editor's value";
compValidationRule.CaseSensitive = true;

CustomValidationRule customValidationRule = new CustomValidationRule();
customValidationRule.ErrorText = "Please enter a valid person name";
customValidationRule.ErrorType = ErrorType.Warning;

dxValidationProvider1.SetValidationRule(containsTextEdit, containsValidationRule);
dxValidationProvider1.SetValidationRule(compareTextEdit, compValidationRule);
dxValidationProvider1.SetValidationRule(customTextEdit, customValidationRule);


private void buttonOk_Click(object sender, EventArgs e) {
    dxValidationProvider1.Validate();
}

// Implements a custom validation rule.
public class CustomValidationRule : ValidationRule {
    public override bool Validate(Control control, object value) {
        string str = (string)value;
        string[] values = new string[] { "Dr.", "Mr.", "Mrs.", "Miss", "Ms." };
        foreach(string val in values) {
            if(ValidationHelper.Validate(str, ConditionOperator.BeginsWith, 
                val, null, null, false)) {
                string name = str.Substring(val.Length);
                if(name.Trim().Length > 0) return true;
            }
        }
        return false;
    }
}
See Also