How do I apply HTML5 Validation with React

0 votes
Can you tell me How do I apply HTML5 Validation with React?
Feb 12 in Node-js by Nidhi
• 11,580 points
85 views

1 answer to this question.

0 votes
You can utilize the built-in HTML5 form validation attributes, such as required, pattern, min, max, type, etc., along with React's controlled form components to manage form data and handle validation.

Code:

import React, { useState } from 'react';

function MyForm() {

  const [formData, setFormData] = useState({

    email: '',

    password: '',

    age: ''

  });

  const handleChange = (e) => {

    const { name, value } = e.target;

    setFormData((prevData) => ({

      ...prevData,

      [name]: value

    }));

  };

  const handleSubmit = (e) => {

    e.preventDefault();

    const form = e.target;

    // HTML5 form validation

    if (form.checkValidity()) {

      console.log('Form submitted successfully');

      // Process form data

    } else {

      console.log('Form validation failed');

      form.reportValidity();

    }

  };

  return (

    <form onSubmit={handleSubmit} noValidate>

      <div>

        <label htmlFor="email">Enter your Email:</label>

        <input

          type="email"

          id="email"

          name="email"

          value={formData.email}

          onChange={handleChange}

          required

        />

      </div>

      <div>

        <label htmlFor="password">Enter correct Password:</label>

        <input

          type="password"

          id="password"

          name="password"

          value={formData.password}

          onChange={handleChange}

          required

          minLength="6"

        />

      </div>

      <div>

        <label htmlFor="age">Enter your Age:</label>

        <input

          type="number"

          id="age"

          name="age"

          value={formData.age}

          onChange={handleChange}

          required

          min="18"

          max="100"

        />

      </div>

      <button type="submit">Done</button>

    </form>

  );

}

export default MyForm;
answered Feb 12 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do I get the path to the current script with Node.js?

Hello @kartik, you can do this: fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,091 views
0 votes
1 answer

How do I determine the current operating system with Node.js

Hello @kartik, With Node.js v6 (and above) there ...READ MORE

answered Jul 14, 2020 in Node-js by Niroj
• 82,840 points
4,027 views
0 votes
1 answer

How do I determine the current operating system with Node.js?

Hello @kartik, With Node.js v6 (and above) there ...READ MORE

answered Jul 20, 2020 in Node-js by Niroj
• 82,840 points
908 views
0 votes
1 answer

How do I create a HTTP Client Request with a cookie?

Hello @kartik, The use of http.createClient is now deprecated. You ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,840 points
6,889 views
0 votes
1 answer

Why does the useEffect hook trigger twice in React?

This behavior is intentional and stems from ...READ MORE

answered Feb 12 in Node-js by Navya
69 views
0 votes
1 answer

Why does React's useState hook use const instead of let?

The useState Hook is typically used with ...READ MORE

answered Feb 12 in Node-js by Navya
95 views
0 votes
1 answer
0 votes
1 answer

Who's responsible for the next View?

In the MVC architecture, the Controller determines ...READ MORE

answered Feb 12 in Node-js by Navya
47 views
0 votes
1 answer
0 votes
1 answer

How do you integrate authentication with React-Router?

To integrate authentication with React Router, follow ...READ MORE

answered Feb 24 in Node-js by Kavya
66 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP