Skip to content

Commit

Permalink
feat: Add email validation and disable submit button for incomplete f…
Browse files Browse the repository at this point in the history
…ields

This code adds real-time validation for email input and disables the submit button when the input is invalid or empty. The form will only be submitted if the email is valid, improving usability.
  • Loading branch information
anjalimahajan2603 authored Oct 31, 2024
1 parent ad9e6da commit bc15c7b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions contactform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ContactForm.js
const validateEmail = (email) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};

const handleSubmit = (e) => {
e.preventDefault();
if (!validateEmail(email)) {
alert("Please enter a valid email.");
return;
}
// proceed with submit actions
};

return (
<form onSubmit={handleSubmit}>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter your email"
/>
<button type="submit" disabled={!validateEmail(email)}>
Submit
</button>
</form>
);

0 comments on commit bc15c7b

Please sign in to comment.