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.
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.
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.
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.
{
"fullName": "Jwala Baheliya",
"email": "jwala@example.com",
"phone": "",
"company": "",
"message": "Need a frontend developer for a landing page."
}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>
);
}// 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"
});
}// 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 });
}How To Use It
- 1Paste code, JSON, markup, or the input the tool needs.
- 2Adjust the options and watch the output update live.
- 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.