The -validate, -validatecommand, and -invalidcommand options are used to enable entry widget validation.

Validation Modes

There are two main validation modes: prevalidation, in which the -validatecommand is evaluated prior to each edit and the return value is used to determine whether to accept or reject the change; and revalidation, in which the -validatecommand is evaluated to determine whether the current value is valid.

The -validate option determines when validation occurs; it may be set to any of the following values:
Default. This means validation will only occur when specifically requested by the validate widget command.
The entry will be prevalidated prior to each edit (specifically, whenever the insert or delete widget commands are called). If prevalidation fails, the edit is rejected.
The entry is revalidated when the entry receives or loses focus.
The entry is revalidated when the entry receives focus.
The entry is revalidated when the entry loses focus.
Validation is performed for all above conditions.

The -invalidcommand is evaluated whenever the -validatecommand returns a false value.

The -validatecommand and -invalidcommand may modify the entry widget’s value via the widget insert or delete commands, or by setting the linked -textvariable. If either does so during prevalidation, then the edit is rejected regardless of the value returned by the -validatecommand.

If -validatecommand is empty (the default), validation always succeeds.

Validation Script Substitutions

It is possible to perform percent substitutions on the -validatecommand and -invalidcommand, just as in a bind script. The following substitutions are recognized:
Type of action: 1 for insert prevalidation, 0 for delete prevalidation, or -1 for revalidation.
Index of character string to be inserted/deleted, if any, otherwise -1.
In prevalidation, the new value of the entry if the edit is accepted. In revalidation, the current value of the entry.
The current value of entry prior to editing.
The text string being inserted/deleted, if any, {} otherwise.
The current value of the -validate option.
The validation condition that triggered the callback (key, focusin, focusout, or forced).
The name of the entry widget.

Differences from Tk Entry Widget Validation

The standard Tk entry widget automatically disables validation (by setting -validate to none) if the -validatecommand or -invalidcommand modifies the entry’s value. The Tk themed entry widget only disables validation if one of the validation scripts raises an error, or if -validatecommand does not return a valid boolean value. (Thus, it is not necessary to re-enable validation after modifying the entry value in a validation script).

In addition, the standard entry widget invokes validation whenever the linked -textvariable is modified; the Tk themed entry widget does not.