I am using Formspree to make a simple Contact form in my NextJS website. Formspree provides the following sample code snippet for React:
// Make sure to run npm install @formspree/react
// For more help visit https://formspr.ee/react-help
import React from 'react';
import { useForm, ValidationError } from '@formspree/react';
function ContactForm() {
const [state, handleSubmit] = useForm("YOUR_FORMSPREE_UNIQUE_KEY");
if (state.succeeded) {
return <p>Thanks for joining!</p>;
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="email">
Email Address
</label>
<input
id="email"
type="email"
name="email"
/>
<ValidationError
prefix="Email"
field="email"
errors={state.errors}
/>
<textarea
id="message"
name="message"
/>
<ValidationError
prefix="Message"
field="message"
errors={state.errors}
/>
<button type="submit" disabled={state.submitting}>
Submit
</button>
</form>
);
}
function App() {
return (
<ContactForm />
);
}
export default App;
Trouble is, if a user submits the form with empty fields, it still gets submitted. I searched the web, looked up some formspree implementations on YT and checked their website. But didn't find a solution on how to prevent a user from submitting with empty fields.