0

I'm using react and antd.
I'm using antd's Input component.
I want to display an Error message at the bottom of the Input form if more than 10 characters are entered.
I want to use antd's errorMessage.

code

import React from "react";
import { Input } from "antd";

const App = () => {
  const [value, setValue] = React.useState(null);
  const [onSave, setOnSave] = React.useState(null);
  const handleInputChange = React.useCallback((e) => {
    setValue(Number(e.target.value));
  }, []);

  const onSaveBlur = React.useCallback(() => {
    if (String(value).length < 10) {
      setOnSave(true);
    } else {
      setOnSave(false);
    }
  }, [value]);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Input
        type="number"
        value={value}
        onChange={handleInputChange}
        onBlur={onSaveBlur}
      ></Input>
    </div>
  );
};

export default App;

0

3 Answers 3

1

Try out this:

  import "./styles.css";
import React from "react";
import { Input, Form } from "antd";

const App = () => {
  const [value, setValue] = React.useState(null);
  const [onSave, setOnSave] = React.useState(null);
  const handleInputChange = React.useCallback((e) => {
    setValue(e.target.value);
  }, []);

  const onSaveBlur = React.useCallback(() => {
    if (String(value).length < 10) {
      setOnSave(true);
    } else {
      setOnSave(false);
    }
  }, [value]);
  return (
    <div className="App">
      <Form>
        <Form.Item
          label="Number"
          name="number"
          rules={[
            { required: true, message: "Please input value!" },
            { max: 10, message: "length should be less then 10 letters!" }
          ]}
        >
          <Input
            type="number"
            value={value}
            onChange={handleInputChange}
            onBlur={onSaveBlur}
          ></Input>
        </Form.Item>
      </Form>
    </div>
  );
};

export default App;

Edit beautiful-danny-f0pc2

Sign up to request clarification or add additional context in comments.

4 Comments

I used antd's errorMessage. ant.design/components/form
@aahhuhuhu Edited. Please check it again.
What is the role of name="username"? When I remove name="username", the error message is no longer displayed.
@aahhuhuhu Nothing, It's just a name and label for the input.
1

You can add Default AntD rule inside Form.Item like below

<Form.Item
   rules = {[{  max:10, message: "length should be less then 10 
   letters!"}]}
>
   <Input placeholder="Username"/>
 </Form.Item>

10 Comments

I copied the above code and used it, but I get an error.
@aahhuhuhu <InputNumber min={1} max={10} value={value} onChange={setValue} /> you can even use InputNumber component of antd it's super simple for your example Refer this: ant.design/components/input-number
I've fixed it, but I'm still getting errors.  codesandbox.io/s/adoring-tree-xe4xm?file=/src/App.js
Check this codesandbox i have edited codesandbox.io/s/adoring-tree-xe4xm?file=/src/App.js
|
0

You should add an error state and display it following the condition:

import React from "react";
import { Input } from "antd";

const App = () => {
  const [value, setValue] = React.useState(null);
  const [onSave, setOnSave] = React.useState(null);
  const [error, setError] = React.useState(null)
  
  const handleInputChange = React.useCallback((e) => {
    setValue(Number(e.target.value));
  }, []);

  const onSaveBlur = React.useCallback(() => {
    if (String(value).length < 10) {
      setOnSave(true);
    } else {
      setError(isLowerCase ? null : 'error message')
      setOnSave(false);
    }
  }, [value]);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Input
        type="number"
        value={value}
        onChange={handleInputChange}
        onBlur={onSaveBlur}
      ></Input>
      <div role="alert" style={{color: 'red'}}>
        {error}
      </div>
    </div>
  );
};

export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

1 Comment

I would like to use errorMessage from the antd library.  ant.design/components/form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.