你可以使用Input组件的onChange事件来实现限制只能输入数字和最大长度为12的功能。代码如下:

import React, { useState } from 'react';
import { Input } from 'antd';

const App = () => {
  const [value, setValue] = useState('');

  const handleChange = (e) => {
    const inputValue = e.target.value;
    const regex = /^[0-9]*$/; // 只允许输入数字
    const maxLength = 12; // 最大长度为12

    if (inputValue.length <= maxLength && regex.test(inputValue)) {
      setValue(inputValue);
    }
  };

  return (
    <Input value={value} onChange={handleChange} />
  );
};

export default App;

在上述代码中,我们使用useState来创建一个名为value的状态,用于保存输入框的值。然后,在onChange事件处理程序中,我们使用正则表达式来验证输入值是否为数字,并且限制输入框的最大长度为12。只有当输入值满足这两个条件时,才更新value的值,从而更新输入框的显示

antd Input输入框如何限制只能输入数字且最大长度为12只用Input 的api

原文地址: https://www.cveoy.top/t/topic/iKz4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录