What is TypeScript and why is it preferred over JavaScript

0 votes
With the help of proper code can you tell me What is TypeScript, and why is it preferred over JavaScript?
Feb 12 in Node-js by Nidhi
• 8,120 points
48 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 Navya

Related Questions In Node-js

0 votes
0 answers
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,842 views
0 votes
0 answers

What is the main difference between REST APIs and GraphQL in a Node.js application?

With the help of code, can you ...READ MORE

Dec 17, 2024 in Node-js by Ashutosh
• 17,360 points
58 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
41 views
0 votes
1 answer
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
46 views
0 votes
0 answers

Should you use MVC 2 or stick with MVC 1 for your project?

Can you tell me Should you use ...READ MORE

Feb 12 in Node-js by Nidhi
• 8,120 points
36 views
0 votes
1 answer

What is the correct method to clone a JavaScript object?

Cloning a JavaScript object can be achieved ...READ MORE

answered Feb 10 in Node-js by Navya
53 views
0 votes
1 answer

What is the process to parse JSON using Node.js?

You can parse JSON data using the ...READ MORE

answered Feb 10 in Node-js by Navya
37 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