0

I am working on react input form but the problem is don't know how to validate, also not getting value on onChange, please have a look at my code.

I have created an array which value I need to update using the input form

useEffect(() => {
    let arr = [];
    for (let i = 0; i < Number(localStorage?.getItem("users")); i++) {
      arr.push({
        username: localStorage.getItem("userName"),
        user_email: localStorage.getItem("userEmail"),
        password: "",
        button: "",
        designation: "",
        country: "",
        city: "", 
        company_name: "",
      });
    }
    setUserRemaining(arr);
  }, []);

then i have input form , which i want with validation

{userRemaining.map((element, index) => {
                  return (
                    <div className="block2">
                      <input type="text" name="username" value='' onChange={(event) =>  handleChange(event, index)}/>
                      
                      <div className="add" onClick={() => addUser(element, index)}>
                        <span>
                          <img src={img1} />
                        </span>
                        Add
                      </div>
                    </div>
                  );
                })}

at last, here is my handleChange method I've tried.

const handleChange = (event, index) => {
    const updatedUser = userRemaining.map((users, index1) => {
      if (index1 == index) {
        return {
          ...users,
          [event.target.name]: event.target.value,
        };
      } else {
        return users;
      }

for more details, I am providing a link to the file too.

3 Answers 3

1

Hello I pretty recommend you use react hooks form https://react-hook-form.com/get-started#Applyvalidation Its pretty easy input validations using this library Here an small example

import React from "react";
import { useForm } from "react-hook-form";

export default function App() {
  const { register, handleSubmit } = useForm();
  const onSubmit = data => console.log(data);
   
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("firstName", { required: true, maxLength: 20 })} />
      <input {...register("lastName", { pattern: /^[A-Za-z]+$/i })} />
      <input type="number" {...register("age", { min: 18, max: 99 })} />
      <input type="submit" />
    </form>
  );
}
Sign up to request clarification or add additional context in comments.

2 Comments

tried but not working in my case, can you please check the link i have given.
ok this is working just want to add errors also to get validation.
0

As Daher said, react hooks form is a great way to validate your inputs.

You can find all the ways you can validate them at https://www.operationdev.com/blog/a-complete-guide-to-react-form-validation/

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
0

React hooks form is a great way to validate your forms but if you need something richer and easier to use, check out this NPM package.

https://www.npmjs.com/package/octavalidate-reactjs

Octavalidate helps me to validate my forms in react seamlessly.

Comments

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.