|
| 1 | +# MaskedInput |
| 2 | + |
| 3 | +!!! tip "Added in version 0.80.0" |
| 4 | + |
| 5 | +A masked input derived from `Input`, allowing to restrict user input and give visual aid via a simple template mask, which also acts as an implicit *[validator][textual.validation.Validator]*. |
| 6 | + |
| 7 | +- [x] Focusable |
| 8 | +- [ ] Container |
| 9 | + |
| 10 | +## Example |
| 11 | + |
| 12 | +The example below shows a masked input to ease entering a credit card number. |
| 13 | + |
| 14 | +=== "Output" |
| 15 | + |
| 16 | + ```{.textual path="docs/examples/widgets/masked_input.py"} |
| 17 | + ``` |
| 18 | + |
| 19 | +=== "checkbox.py" |
| 20 | + |
| 21 | + ```python |
| 22 | + --8<-- "docs/examples/widgets/masked_input.py" |
| 23 | + ``` |
| 24 | + |
| 25 | +## Reactive Attributes |
| 26 | + |
| 27 | +| Name | Type | Default | Description | |
| 28 | +| ---------- | ----- | ------- | ------------------------- | |
| 29 | +| `template` | `str` | `""` | The template mask string. | |
| 30 | + |
| 31 | +### The template string format |
| 32 | + |
| 33 | +A `MaskedInput` template length defines the maximum length of the input value. Each character of the mask defines a regular expression used to restrict what the user can insert in the corresponding position, and whether the presence of the character in the user input is required for the `MaskedInput` value to be considered valid, according to the following table: |
| 34 | + |
| 35 | +| Mask character | Regular expression | Required? | |
| 36 | +| -------------- | ------------------ | --------- | |
| 37 | +| `A` | `[A-Za-z]` | Yes | |
| 38 | +| `a` | `[A-Za-z]` | No | |
| 39 | +| `N` | `[A-Za-z0-9]` | Yes | |
| 40 | +| `n` | `[A-Za-z0-9]` | No | |
| 41 | +| `X` | `[^ ]` | Yes | |
| 42 | +| `x` | `[^ ]` | No | |
| 43 | +| `9` | `[0-9]` | Yes | |
| 44 | +| `0` | `[0-9]` | No | |
| 45 | +| `D` | `[1-9]` | Yes | |
| 46 | +| `d` | `[1-9]` | No | |
| 47 | +| `#` | `[0-9+\-]` | No | |
| 48 | +| `H` | `[A-Fa-f0-9]` | Yes | |
| 49 | +| `h` | `[A-Fa-f0-9]` | No | |
| 50 | +| `B` | `[0-1]` | Yes | |
| 51 | +| `b` | `[0-1]` | No | |
| 52 | + |
| 53 | +There are some special characters that can be used to control automatic case conversion during user input: `>` converts all subsequent user input to uppercase; `<` to lowercase; `!` disables automatic case conversion. Any other character that appears in the template mask is assumed to be a separator, which is a character that is automatically inserted when user reaches its position. All mask characters can be escaped by placing `\` in front of them, allowing any character to be used as separator. |
| 54 | +The mask can be terminated by `;c`, where `c` is any character you want to be used as placeholder character. The `placeholder` parameter inherited by `Input` can be used to override this allowing finer grain tuning of the placeholder string. |
| 55 | + |
| 56 | +## Messages |
| 57 | + |
| 58 | +- [MaskedInput.Changed][textual.widgets.MaskedInput.Changed] |
| 59 | +- [MaskedInput.Submitted][textual.widgets.MaskedInput.Submitted] |
| 60 | + |
| 61 | +## Bindings |
| 62 | + |
| 63 | +The masked input widget defines the following bindings: |
| 64 | + |
| 65 | +::: textual.widgets.MaskedInput.BINDINGS |
| 66 | + options: |
| 67 | + show_root_heading: false |
| 68 | + show_root_toc_entry: false |
| 69 | + |
| 70 | +## Component Classes |
| 71 | + |
| 72 | +The masked input widget provides the following component classes: |
| 73 | + |
| 74 | +::: textual.widgets.MaskedInput.COMPONENT_CLASSES |
| 75 | + options: |
| 76 | + show_root_heading: false |
| 77 | + show_root_toc_entry: false |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | + |
| 82 | +::: textual.widgets.MaskedInput |
| 83 | + options: |
| 84 | + heading_level: 2 |
0 commit comments