Material-ui: Textfield type="number" allowing non-numeric values in firefox

Created on 19 Dec 2019  路  4Comments  路  Source: mui-org/material-ui

Current Behavior 馃槸

When creating a number field like the following: <Textfield type="number" /> on firefox it allows non-numeric values. It works fine in chrome.

Expected Behavior 馃

When creating a number field like the following: <Textfield type="number" /> it should only allow numeric values.

Steps to Reproduce 馃暪

Steps:

  1. Create a Textfield with type="number"
  2. Open the app in firefox
  3. Try to type a

Your Environment 馃寧

| Tech | Version |
| ----------- | ------- |
| Material-UI | v4.8.0 |
| React | v 16.12 |
| Browser |Firefox v71|
| TypeScript | 3.7.2 |
| etc. | |

TextField duplicate

Most helpful comment

@BjoernRave as a workaround you can do this:

React.useEffect(() => {
    document
      .querySelector("input[type='number']")
      .addEventListener("keypress", evt => {
        if (evt.which === 8) {
          return;
        }
        if (evt.which < 48 || evt.which > 57) {
          evt.preventDefault();
        }
      });
  }, []);

All 4 comments

@BjoernRave This is not Material bug. There seems to be some disagreement about this among browsers
Link 1
Link 2
Link 3

@BjoernRave as a workaround you can do this:

React.useEffect(() => {
    document
      .querySelector("input[type='number']")
      .addEventListener("keypress", evt => {
        if (evt.which === 8) {
          return;
        }
        if (evt.which < 48 || evt.which > 57) {
          evt.preventDefault();
        }
      });
  }, []);

@mmarkelov okay, didn't know that and also thanks for the workaround, I was almost going crazy :D

Duplicate of #10582

Was this page helpful?
0 / 5 - 0 ratings

Related issues

newoga picture newoga  路  3Comments

rbozan picture rbozan  路  3Comments

FranBran picture FranBran  路  3Comments

mb-copart picture mb-copart  路  3Comments

sys13 picture sys13  路  3Comments