惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
腾讯CDC
B
Blog RSS Feed
H
Help Net Security
J
Java Code Geeks
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
博客园_首页
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
博客园 - Franky
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 叶小钗
Martin Fowler
Martin Fowler

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
React Form Component – Blog Explanation with Code
Jayashree · 2026-06-17 · via DEV Community
Cover image for React Form Component – Blog Explanation with Code

Jayashree

This component is a simple user registration form built using React. It shows how to manage form data using state, handle input changes, and perform basic validation.

import { useState } from "react";

function FormValidation() {
  const [userdata, setUserdata] = useState({
    name: "",
    age: "",
    mobile: "",
    email: "",
    dob: "",
    password: ""
  });

  // Handle input change
  function getdata(e) {
    const { name, value } = e.target;

    setUserdata({
      ...userdata,
      [name]: value
    });
  }

  // Validate form
  function validate() {
    const { name, age, mobile, email, dob, password } = userdata;

    if (name && age && mobile && email && dob && password) {
      alert("Submitted successfully...");
    } else {
      alert("Please fill all fields");
    }
  }

  // Handle Enter key submit
  function handlekey(e) {
    if (e.key === "Enter") {
      validate();
    }
  }

  return (
    <div>
      <form onKeyDown={handlekey}>
        <label>Enter your name</label><br />
        <input
          name="name"
          type="text"
          onChange={getdata}
          value={userdata.name}
        /><br />

        <label>Enter your age</label><br />
        <input
          name="age"
          type="text"
          onChange={getdata}
          value={userdata.age}
        /><br />

        <label>Enter your mobile</label><br />
        <input
          name="mobile"
          type="number"
          onChange={getdata}
          value={userdata.mobile}
        /><br />

        <label>Enter your email</label><br />
        <input
          name="email"
          type="email"
          onChange={getdata}
          value={userdata.email}
        /><br />

        <label>Enter your date of birth</label><br />
        <input
          name="dob"
          type="date"
          onChange={getdata}
          value={userdata.dob}
        /><br />

        <label>Create password</label><br />
        <input
          name="password"
          type="password"
          onChange={getdata}
          value={userdata.password}
        /><br />

        <button type="button" onClick={validate}>
          Submit
        </button>
      </form>
    </div>
  );
}

export default FormValidation;

State Management

We store all form fields inside a single state object using useState.

import { useState } from "react";

function FormValidation() {
  const [userdata, setUserdata] = useState({
    name: "",
    age: "",
    mobile: "",
    email: "",
    dob: "",
    password: ""
  });

Handling Input Changes

Each input updates only its own field in the state.

function getdata(inputdata) {
    let data = inputdata.target;

    setUserdata({
      ...userdata,
      [data.name]: data.value
    });
  }

Validation Logic

Before submitting, we check whether all fields are filled.

function validate() {
    let { name, age, mobile, email, dob, password } = userdata;

    if (name && age && mobile && email && dob && password) {
      alert("submitted...");
    } else {
      alert("please fill all fields");
    }
  }

Handling Enter Key

This is used to submit the form using the keyboard (Enter key).

  function handlekey(key) {
    if (key.key === "Enter") {
      validate();
    }
  }

Form UI

All inputs are controlled components connected to state.

return (
    <div>
      <form>
        <label>Enter your name</label><br />
        <input
          name="name"
          type="text"
          onChange={getdata}
          value={userdata.name}
        /><br />

        <label>Enter your age</label><br />
        <input
          name="age"
          type="text"
          onChange={getdata}
          value={userdata.age}
        /><br />

        <label>Enter your mobile</label><br />
        <input
          name="mobile"
          type="number"
          onChange={getdata}
          value={userdata.mobile}
        /><br />

        <label>Enter your email</label><br />
        <input
          name="email"
          type="email"
          onChange={getdata}
          value={userdata.email}
        /><br />

        <label>Enter your date of birth</label><br />
        <input
          name="dob"
          type="date"
          onChange={getdata}
          value={userdata.dob}
        /><br />

        <label>Create password</label><br />
        <input
          name="password"
          type="password"
          onChange={getdata}
          value={userdata.password}
        /><br />

        <button type="button" onClick={validate}>
          Submit
        </button>
      </form>
    </div>
  );
}

export default FormValidation;

Conclusion

This React component demonstrates:

  • Controlled form inputs
  • State management using useState
  • Dynamic field update using input name
  • Basic form validation
  • Simple user interaction handling

With small improvements, this can be extended into a real-world registration system with backend integration.