number
Numeric input with validation, min/max bounds, and step controls.
Preview
Typing
number
◇ Port number?│ 8080└
Validation error
number
▲ Port number?│ 99999▲ Must be at most 65535.
Usage
import { number, isCancel } from 'termprompt';
const port = await number({
message: 'Port number?',
min: 1,
max: 65535,
});
if (isCancel(port)) process.exit(0);
console.log(port); // number
Props
| Prop | Type | Default | Description |
|---|---|---|---|
message | string | required | Question text |
min | number? | none | Minimum allowed value |
max | number? | none | Maximum allowed value |
step | number | 1 | Increment/decrement step |
initialValue | number? | none | Pre-filled value |
placeholder | string? | none | Gray text shown when empty |
validate | (value: number) => true | string | none | Return true or an error message |
Step Controls
Use Up/Down arrow keys to increment or decrement by the step value. The value is clamped to min/max bounds.
const quantity = await number({
message: 'How many?',
min: 1,
max: 100,
step: 5,
initialValue: 10,
});
Validation
Built-in validation checks min/max bounds on submit. You can add custom validation on top.
const even = await number({
message: 'Enter an even number',
validate(value) {
if (value % 2 !== 0) return 'Must be even';
return true;
},
});
Keyboard
| Key | Action |
|---|---|
0-9, -, . | Type digits |
Up | Increment by step |
Down | Decrement by step |
Left / Right | Move cursor |
Home / Ctrl+A | Go to start |
End / Ctrl+E | Go to end |
Ctrl+U | Clear line |
Ctrl+W | Delete word back |
Backspace | Delete character |
Delete | Delete forward |
Enter | Submit |
Esc | Cancel |