Forem: Can't enter custom listing credits amount

Created on 26 Apr 2019  路  3Comments  路  Source: forem/forem

Describe the bug
In the buying credits for listing page, the amount input can't be filled using number key on keyboard.
On the JS code, there's:

document.getElementById("amount-input").addEventListener("keydown",function (e) {
    if ((e.keyCode < 48 || e.keyCode > 57) && e.keyCode != 8) {
        e.preventDefault();
    }
    calculatePriceAndShow();
});

The preventDefault function is wrongfully called. I don't get what it's used for anyway.

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://dev.to/credits/new
  2. Click on 'Amount to purchase' input
  3. Try entering custom amount using keyboard numbers
  4. Nothing append

Expected behavior
I should be able to enter any amount.

listings bug

All 3 comments

@GMartigny the condition is there to only allow English numbers. If it's not there you can input any number from other languages. I was able to put 郾鄄鄢鄞鄣鄱鄯鄹酃郯(which is 1234567890 in Farsi) without the condition and it's still valid number. Take a look below:
Screenshot_20190503_144503

However, the condition won't allow for keypad numbers because keypad 0-9 is keycode 96 to 105. I believe the correct function should be this:

function listenForNumberChange() {
  document.getElementById("amount-input").addEventListener("keydown",function (e) {
    (
      (e.keyCode >= 48 && e.keyCode <= 57) || // Keyboard 0-9 Keycode is 48 to 57
      (e.keyCode >= 96 && e.keyCode <= 105) || //Keypad 0-9 Keycode is 96 to 105
      (e.keyCode == 8) //Allow backspace

    ) ? 
      calculatePriceAndShow():
      e.preventDefault();
  });
}

Ok, but why prevent typing farsi numbers ? JS seems to understand the input value correctly and display the right amount (3086419725 $) on the right of the input.

image

IMO, the website should not prevent user actions, but they should be sanitized on the back-end.

resolved with PR #2672

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Zhao-Andy picture Zhao-Andy  路  3Comments

NansD picture NansD  路  3Comments

szabgab picture szabgab  路  3Comments

michael-tharrington picture michael-tharrington  路  3Comments

saurabhdaware picture saurabhdaware  路  3Comments