添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

how to set maxlength in input type number in react js

In React JS, you can set the maximum length of an input of type "number" by using the "maxLength" attribute.

Here's an example of how you can use it in a component:

class MyComponent extends React.Component {
  render() {
    return (
      <input type="number" maxLength={4} />

This will set the maxLength of input to 4. You can also set the max length by using the input's ref and setting the maxLength property on the DOM element.

class MyComponent extends React.Component {
  inputRef = React.createRef();
  componentDidMount() {
    this.inputRef.current.maxLength = 4;
  render() {
    return (
      <input type="number" ref={this.inputRef} />

Note that maxLength only works on input type text, password and search, so it will not work on input type number. you can use max and min attributes to set the max and min value of the input.

  •