📜 ⬆️ ⬇️

Enter fractional values ​​without changing the layout

How often do you have to enter fractional values ​​into the interface of any program / web service? If often, then you have probably come across inadequate behavior of such fields. For example, I quite regularly beat my forehead with absolutely stupid forms. Want to know why entering fractional values ​​can lead to a white-hot, and what to do about it? Welcome to cat.

According to my observations, the following algorithms of their behavior are most common:



In general, it's the 21st century, the interfaces are developing, and such trivial actions as entering fractional values ​​are still a headache for users. A few years ago I had a chance to design (and partially code) a small program, for users of which entering fractional values ​​was a daily task. Naturally, I did not want to repeat the behaviors described above. As a result of brief deliberation, a rather simple algorithm was born.
')
So what to do with fractional numbers? First of all, you must filter the input. Verification when sending a form is a good thing, but it is much more convenient for the user if the value is entered the first time, and not after a kick from the script. Here is my version of this filter:

  1. Allow the input of numbers from 0 to 9.
  2. When you press any button that carries any possible decimal separator in any layout, you need to replace the symbol sent from the keyboard so that the correct decimal separator gets into the input field.
  3. blocking an attempt to enter more than one decimal separator.
  4. we limit the number of characters in the fractional part of the input number depending on the type of input data.

Example 1. A user on the Russian layout consistently presses the keys [1] [2] [3] [b] [4] [5]. Result in the input field: 123.45

Example 2. The user on the English layout sequentially presses the keys [1] [2] [3] [Shift +?] [4] [5]. Result in the input field: 123.45

The result of this algorithm gives a great (wah) effect. Users no longer need to think about the layout, press Shift and guess, the programmer took the decimal point or comma for decimal. Experiments on live users have shown that after 1-2 weeks of working with such a “smart” input field, returning to regular fields causes a break and the desire to kill the programmer to break the keyboard.

Ps. Honestly, this algorithm is so simple that I am 146% sure that this approach has been used more than once or twice. But for unknown reasons, it was not widely used (if you do not believe, find the nearest input field and check its behavior - it is almost certain that it will fit into one of the three “clumsy” algorithms, and not into the “correct” result).

Pps. For those who write in Delphi or Lazarus I will provide a link to the CurrencyEdit component that implements this algorithm in the appropriate language: GitHub .

PPPS. Specific examples do not cite solely out of respect in the authors of the relevant programs. If someone from Habr's readers finds “wrong” algorithms in his code and decides to change them to “correct” - the gratitude from users is guaranteed.

PPPPS. Somehow I'm not quite in the topic: in HTML-forms, in principle, you can filter the input?

Source: https://habr.com/ru/post/233647/


All Articles