Toolkit
Hire me
JavaScript

Forms Lab: Beginner to Advanced

Forms Lab: Beginner to Advanced in Jwala Baheliya's frontend toolkit. A live javascript utility with copy-ready output and in-browser previews.

Visual Guide
A cleaner, more guided way to use Forms Lab: Beginner to Advanced.

The left rail keeps navigation nearby, the right rail keeps your reading position visible, and the live tool stays front and center once you are ready to use it.

Category
JavaScript
Mode
Interactive docs
Route
/forms-lab
What It Does
Forms Lab: Beginner to Advanced is useful when you need frontend logic, data conversion, or browser-side code output that you can test and reuse quickly.
How To Use
Paste code, JSON, markup, or the input the tool needs. Then adjust the options and watch the output update live.
What You Get
Copy-ready frontend code, transformed data, or browser-friendly output.

What This Tool Does

Forms Lab: Beginner to Advanced is useful when you need frontend logic, data conversion, or browser-side code output that you can test and reuse quickly.

If you are not sure where to start, use the live controls below, watch the preview change, and copy the result only when it matches what you want in your project.

Live Tool

Interactive, client-side, and still running directly inside this docs page.

All tools
Full form flow

One complete form, from validation to submit

This lab now focuses on one practical flow. Build the form, validate it, submit it to an API, handle success and errors, and then extend the same pattern to Next.js, React, vanilla JavaScript, or SFDC relay flows.

1. Fill form
User types into clearly labeled fields.
2. Validate
Client checks required fields and formats.
3. Submit
Frontend sends JSON to your API route.
4. Verify
Server validates again and applies guardrails.
5. Save
Server saves to DB or forwards safely to SFDC.
6. Respond
Frontend shows success or field-level errors.
Current setup
react form using `/api/contact`
`work` changes the visible fields and validation depth, so the code stays easier to follow.
Live form preview
Validate first, then submit
Honeypot stays hidden in production, but shown here so the flow is easier to understand.
ready
What happens here
Client validation is for UX. Server validation is for trust.
CSRF example is enabled. Use it for cookie-based auth flows.
Honeypot example is enabled for basic bot filtering.
Always design loading, success, and error states before wiring the API.
Never trust hidden fields or client-side role checks for permission logic.
Submitted payload
{
  "fullName": "Jwala Baheliya",
  "email": "jwala@example.com",
  "phone": "",
  "company": "",
  "message": "Need a frontend developer for a landing page."
}
tsx
import { useState } from "react";

export function ContactForm() {
  const [form, setForm] = useState({
  fullName: "Jwala Baheliya",
  email: "jwala@example.com",
  phone: "",
  company: "",
  message: "Need a frontend developer for a landing page.",
});
  const [errors, setErrors] = useState<Record<string, string>>({});
  const [loading, setLoading] = useState(false);
  const [status, setStatus] = useState("");

  const handleChange = (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
    const { name, value } = event.target;
    setForm((current) => ({ ...current, [name]: value }));
    setErrors((current) => ({ ...current, [name]: "" }));
  };

  const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    const nextErrors: Record<string, string> = {};
    if (!form.fullName.trim()) nextErrors.fullName = "Full name is required.";
    if (!form.email.trim()) nextErrors.email = "Email is required.";
    else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email)) nextErrors.email = "Enter a valid email address.";
    if (form.phone && !/^[0-9+()\-\s]{7,20}$/.test(form.phone)) nextErrors.phone = "Phone should be 7 to 20 characters.";
    if (!form.message.trim()) nextErrors.message = "Message is required.";

    if (Object.keys(nextErrors).length) {
      setErrors(nextErrors);
      setStatus("Fix validation errors first.");
      return;
    }

    setLoading(true);
    setStatus("Submitting...");

    try {
      const response = await fetch("/api/contact", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "X-CSRF-Token": csrfToken,
        },
        body: JSON.stringify(form),
      });

      if (!response.ok) throw new Error("Request failed");
      setStatus("Form submitted successfully.");
    } catch {
      setStatus("Submission failed.");
    } finally {
      setLoading(false);
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <label>
        <span>Full name</span>
        <input
          name="fullName"
          type="text"
          value={form.fullName}
          onChange={handleChange}
          placeholder="Jwala Baheliya"
          aria-invalid={Boolean(errors.fullName)}
        />
        {errors.fullName && <small>{errors.fullName}</small>}
      </label>

      <label>
        <span>Email</span>
        <input
          name="email"
          type="email"
          value={form.email}
          onChange={handleChange}
          placeholder="you@example.com"
          aria-invalid={Boolean(errors.email)}
        />
        {errors.email && <small>{errors.email}</small>}
      </label>

      <label>
        <span>Phone</span>
        <input
          name="phone"
          type="tel"
          value={form.phone}
          onChange={handleChange}
          placeholder="+91 98765 43210"
          aria-invalid={Boolean(errors.phone)}
        />
        {errors.phone && <small>{errors.phone}</small>}
      </label>

      <label>
        <span>Company</span>
        <input
          name="company"
          type="text"
          value={form.company}
          onChange={handleChange}
          placeholder="Acme Inc."
          aria-invalid={Boolean(errors.company)}
        />
        {errors.company && <small>{errors.company}</small>}
      </label>

      <label>
        <span>Message</span>
        <textarea
          name="message"
          value={form.message}
          onChange={handleChange}
          placeholder="Tell me about the project"
          aria-invalid={Boolean(errors.message)}
        />
        {errors.message && <small>{errors.message}</small>}
      </label>
      <input type="text" name="website" hidden tabIndex={-1} autoComplete="off" />
      <button type="submit" disabled={loading}>{loading ? "Submitting..." : "Submit"}</button>
    </form>
  );
}
ts
// backend route idea
export async function POST(request: Request) {
  const body = await request.json();

  // 1. validate body
  // 2. auth / CSRF / rate limit
  // 3. save to database or CRM

  return Response.json({
    ok: true,
    message: "Saved successfully"
  });
}
ts
// safer SFDC / Salesforce relay
export async function POST(request: Request) {
  const body = await request.json();

  const payload = new URLSearchParams({
    oid: process.env.SFDC_ORG_ID ?? "",
    first_name: body.fullName,
    email: body.email,
    phone: body.phone ?? "",
    company: body.company ?? "",
    description: body.message,
  });

  await fetch("https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8", {
    method: "POST",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: payload.toString(),
  });

  return Response.json({ ok: true });
}
Client validation
Use field-level errors, instant feedback, and accessible labels so users know what is wrong before submit.
Server validation
Repeat validation on the server, because browser checks can always be bypassed.
Submit states
Design `loading`, `success`, and `error` states from the start, not as an afterthought.

How To Use It

  1. 1Paste code, JSON, markup, or the input the tool needs.
  2. 2Adjust the options and watch the output update live.
  3. 3Copy the generated code and drop it into your project.

Related Tools

More tools from the same category, so you can keep moving without going back to the full index.

Browse all
Quick nav
Forms Lab: Beginner to Advanced
JavaScript