Material-ui: Text field maxLength for input="number" won't work

Created on 1 Oct 2016  路  4Comments  路  Source: mui-org/material-ui

Problem description

MaxLength works fine for input="text" but it doesn't for input="number". I imagined that max="" would work in this case, but that didn't work either.

Steps to reproduce

Versions

  • Material-UI:
  • React:
  • Browser:
bug 馃悰 TextField

Most helpful comment

If anyone comes here trying to find out how to apply a minimum value to a number TextField in material-ui@next, look no further

<TextField
  inputProps={{
    maxLength: 0,
  }}
/>

All 4 comments

Hello, is there a solution for this in the meanwhile ?

Unfortunately this is default behaviour In HTML5 (Link to Thread) although I got it working with a small hack:

<TextField type="number"
    className="text-field-amount"
    onInput={(e)=>{ 
        e.target.value = Math.max(0, parseInt(e.target.value) ).toString().slice(0,2)
    }}
    min={0}
/>

So in this example values between 0-to-99, but you can change it to (0,35)for 35 character long numbers. From an UX view, it should not default to 0, and instead be processed later e.g
(value="")? 0 : value

Hope that helps.

If anyone comes here trying to find out how to apply a minimum value to a number TextField in material-ui@next, look no further

<TextField
  inputProps={{
    maxLength: 0,
  }}
/>

I threw this into mine and it seems to work with type="number"

<FormControl
  ...
>
    <Input
      ...
      type="number"
    }
    inputProps={{
      min: "0",
    }}
  />
</FormControl>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bessonov picture Bessonov  路  93Comments

damianobarbati picture damianobarbati  路  55Comments

garygrubb picture garygrubb  路  57Comments

nathanmarks picture nathanmarks  路  100Comments

amcasey picture amcasey  路  70Comments