[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to read an integer from the minibuffer

From: Yuri Khan
Subject: Re: How to read an integer from the minibuffer
Date: Fri, 12 Nov 2021 14:21:32 +0700

On Thu, 11 Nov 2021 at 14:19, Marcin Borkowski <> wrote:

> >> How do I do that?  One way would be to use `read-from-minibuffer' with
> >> a suitable keymap, but that seems slightly low-level.  If that is the
> >> way to go, is there a good way to set up a keymap so that nothing except
> >> some specified characters are self-inserting?  IOW, is `suppress-keymap'
> >> the way to go or is there some other way?
> >
> > Perhaps you're looking for `read-number'?
> Thanks, that is nice, but not very general.  Another value I want to
> read is a time in the hh:mm format, for example.

There are lots of examples out there of form controls trying to
prevent the user from entering invalid data, and they demonstrate many
things you can get wrong while implementing one.

A form control for integer numbers only permits typing digits (as you
initially asked). Now the user cannot enter -42.

Okay, after a bug report and a fix, the control now permits the minus
sign. But the control validates the entry after every keypress, so, as
soon as the user starts typing, they are presented with an error
message, “‘-’ is not a valid integer”. Well, duh, I just started
typing, let me finish it!

My bank’s money transfer web form’s amount field filters keypresses to
only allow digits. One, I cannot paste a number I copied from an
invoice opened elsewhere. Two, I cannot press Ctrl+L to activate my
browser’s address bar, or switch to another tab with Ctrl+PgUp,
Ctrl+PgDn, or Alt+[1…9]. Three, I can’t move my cursor if I
inadvertently skip or mistype a digit, e.g., to correct a 1000 to
15000, I cannot type ‘← ← ← 5’, I have to type ‘backspace backspace
backspace 5 0 0 0’.

A license activation input box in a proprietary software installer
wants a string of 20 semi-random letters. Since no one can reliably
enter that in one go, it splits those 20 letters into 4 groups of 5
and presents them as separate input boxes. As a courtesy to the user,
as soon as 5 letters are typed in the first box, the cursor
automatically moves to the second box. However, if you mistype the
last of those 5 letters, Backspace does not delete it, because
technically you’re in the second box and it’s technically empty,
there’s nothing to delete. Also, you can’t paste the whole 20-letter
code in one go, because the first box is limited to 5 letters.

A bank money transfer is confirmed with a 4-digit code that is sent in
an SMS message. As soon as you type the 4th digit, it goes on to
verify the code, without you having to press Enter. However, this
means you cannot immediately correct a mistyped 4th digit; you get a
“verification failed” message first.

Guideline: Perform input validation only after the user has indicated
they are finished with data entry.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]