gpt4 book ai didi

reactjs - 如何在此 ant design 登录表单中实现最少 5 个字符的验证?我找不到示例

转载 作者:行者123 更新时间:2023-12-03 20:15:19 25 4
gpt4 key购买 nike

我查看了 ant.design 表单的所有示例,但找不到文本框最小长度的验证。你能帮忙吗?

https://ant.design/components/form/?locale=en-US#header

https://codesandbox.io/s/pww70x7y6q

import { Form, Icon, Input, Button, Checkbox } from 'antd';

class NormalLoginForm extends React.Component {
handleSubmit = e => {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
};

render() {
const { getFieldDecorator } = this.props.form;
return (
<Form onSubmit={this.handleSubmit} className="login-form">
<Form.Item>
{getFieldDecorator('username', {
rules: [{ required: true, message: 'Please input your username!' }],
})(
<Input
prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
placeholder="Username"
/>,
)}
</Form.Item>
<Form.Item>
{getFieldDecorator('password', {
rules: [{ required: true, message: 'Please input your Password!' }],
})(
<Input
prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
type="password"
placeholder="Password"
/>,
)}
</Form.Item>
<Form.Item>
{getFieldDecorator('remember', {
valuePropName: 'checked',
initialValue: true,
})(<Checkbox>Remember me</Checkbox>)}
<a className="login-form-forgot" href="">
Forgot password
</a>
<Button type="primary" htmlType="submit" className="login-form-button">
Log in
</Button>
Or <a href="">register now!</a>
</Form.Item>
</Form>
);
}
}

const WrappedNormalLoginForm = Form.create({ name: 'normal_login' })(NormalLoginForm);

ReactDOM.render(<WrappedNormalLoginForm />, mountNode);

如何在此 ant design 登录表单中实现最少 5 个字符的验证?我找不到示例。

最佳答案

您可以为输入添加更多规则:

<Form.Item>
{getFieldDecorator('username', {
rules: [
{ required: true, message: 'Please input your username!' },
{ min: 5, message: 'Username must be minimum 5 characters.' },
],
})(
<Input
prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
placeholder="Username"
/>,
)}
</Form.Item>

有关其他验证规则,请访问 here
希望这可以帮助。

关于reactjs - 如何在此 ant design 登录表单中实现最少 5 个字符的验证?我找不到示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57105076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com