# onValidateValue

> An event raised to validate or adjust the field's value before committing it.

## Definition

* **Type:** Event
* **Namespace:** [UnityEngine.UIElements](/engine/6000.7/script-reference/unityengine/uielements.md)
* **Assembly:** UnityEngine.UIElementsModule

```csharp
public event BaseField<TValueType>.ValidateValueHandler onValidateValue
```

### Remarks

Use this event to modify or validate a value before committing it. For example, you can clamp, sanitize, or replace the input value to match specific constraints. To reject an input value, return a fallback such as the current [\_value](/engine/6000.7/script-reference/unityengine/uielements/basefield1/value.md).

Registered callbacks run whenever the field's value changes, either through the [\_value](/engine/6000.7/script-reference/unityengine/uielements/basefield1/value.md) setter or [BaseField\<TValueType>.SetValueWithoutNotify](/engine/6000.7/script-reference/unityengine/uielements/basefield1/setvaluewithoutnotify.md). The value returned by each callback replaces the incoming value and is then stored. If the change occurs from the [\_value](/engine/6000.7/script-reference/unityengine/uielements/basefield1/value.md) setter, it reports the stored value as [\_newValue](/engine/6000.7/script-reference/unityengine/uielements/changeevent1/newvalue.md). If the change occurs from [BaseField\<TValueType>.SetValueWithoutNotify](/engine/6000.7/script-reference/unityengine/uielements/basefield1/setvaluewithoutnotify.md), it doesn't report a [ChangeEvent\<T>](/engine/6000.7/script-reference/unityengine/uielements/changeevent1.md).

If you register more than one callback, they compose in their registration order. Each callback receives the previous callback's result, and the final result is stored and reported.

To assign a value while bypassing these callbacks, wrap the assignment in [BaseField\<TValueType>.IgnoreValidation()](/engine/6000.7/script-reference/unityengine/uielements/basefield1/ignorevalidation.md).

The following example clamps a field's value to a maximum before returning it:

### Examples

```csharp
using UnityEngine;
using UnityEngine.UIElements;

namespace MyNamespace
{
    public class BaseFieldValidateValueExample
    {
        // Clamps the field's value to a maximum before it is committed.
        public IntegerField CreateCappedField()
        {
            var field = new IntegerField("Capped value");
            const int max = 1000;

            field.onValidateValue += v => Mathf.Min(v, max);

            return field;
        }
    }
}
```
