Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have an antd
InputNumber
in
Form.Item
in a react project.
My validation for this input is checking the length of input value.
This is my code:
render() {
return (
<Form.Item
label="Value"
name="numberValue"
rules={[
pattern: /^(?:\d*)$/,
message: "Value should contain just number",
maxLength: 50,
message: "Value should be less than 50 character",
validateTrigger="onBlur"
<InputNumber
onChange={(value) => {
this.props.setValue(value);
</Form.Item>
I have two problems:
I want to show the Value should contain just number
messages when user enter non numeric character. But this message doesn't show at all.
I want to show the Value should be less than 50 character
message, when user enter the number/value with more than 10 character. But now, with entering the first character, this message will be shown!
Depends on which validation library are you using.
InputNumber
max is Number.MAX_SAFE_INTEGER
, so maybe use a simple <Input>
with a pattern matcher:
render() {
return (
<Form.Item
label="Value"
name="numberValue"
rules={[
pattern: /^(?:\d*)$/,
message: "Value should contain just number",
pattern: /^[\d]{0,50}$/,
message: "Value should be less than 50 character",
validateTrigger="onBlur"
<Input
onChange={(value) => {
this.props.setValue(value);
</Form.Item>
–
–
–
You use maxlength in <Input />
and type='number'
must in rules
<Input maxLength={50}
onChange={e => getValue(e.target.value)}
rules={[{ type: 'number' }]} />
The native input tag it self won't support input tag which has type='number' to use maxLength. MaxLength works with text.
Link to the Github Ant Design issue.
Ant Design has nothing to do with this.
Instead you can use as bellow code to limit the number.
<Input
onChange={(e) =>
e.target.value.length <= 10 &&
handleChange(e.target.value)
type='number'
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.