简体   繁体   中英

How do I limit possible inputs of String field in TextField component of Material-UI?

In HTML5 we can do <input type="text" maxlength="3" /> but how do I same in Material-UI ? Bellow is example Material-UI TextFiled component

<TextField
          id="name"
          label="Name"
          type="string"
          //maxLength="3" Or maxlength="3" Or max="3"
          margin="normal"
        />

Add the inputProps to the TextField example following:

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

Or alternative hard coded solution:

<TextField
  onInput={e => {
    e.target.value = Math.max(0, parseInt(e.target.value)).toString().slice(0, 12);
  }}
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM