ReactJS Program to Email Form With Simple Validation with Explanation
ReactJS
Medium
Forms & Validation
30 views
1 min read
91 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around email, form, show. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Create an email form that shows an error if input is empty or invalid.
Input Format
No input.
Output Format
Render a React component.
Constraints
Validate on submit and show a message.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { useState } from 'react';
function isEmail(value) {
return /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(value);
}
export default function App() {
const [email, setEmail] = useState('');
const [error, setError] = useState('');
const [done, setDone] = useState(false);
function submit(e) {
e.preventDefault();
setDone(false);
if (!email.trim()) {
setError('Email is required');
return;
}
if (!isEmail(email.trim())) {
setError('Enter a valid email');
return;
}
setError('');
setDone(true);
}
return (
<div style={{ padding: 16, width: 420 }}>
<h2 style={{ marginTop: 0 }}>meetcode newsletter</h2>
<form onSubmit={submit} style={{ display: 'grid', gap: 10 }}>
<label>
Email
<div>
<input value={email} onChange={(e) => setEmail(e.target.value)} style={{ padding: '10px 12px', borderRadius: 12, border: '1px solid ' + (error ? '#c1121f' : '#bbb'), width: '100%' }} />
</div>
</label>
<button type='submit' style={{ padding: '10px 14px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>Subscribe</button>
</form>
{error ? <div style={{ marginTop: 10, color: '#c1121f' }}>{error}</div> : null}
{done ? <div style={{ marginTop: 10, color: '#0b5' }}>Saved on meetcode.</div> : null}
</div>
);
}
Output Example
No sample I/O is provided for this question.
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Create an email form that shows an error if input is empty or invalid.
Input / Output
Output
Render a React component.
Constraints
Validate on submit and show a message.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Difficulty
Medium
ReactJS
Official Solution
import React, { useState } from 'react';
function isEmail(value) {
return /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(value);
}
export default function App() {
const [email, setEmail] = useState('');
const [error, setError] = useState('');
const [done, setDone] = useState(false);
function submit(e) {
e.preventDefault();
setDone(false);
if (!email.trim()) {
setError('Email is required');
return;
}
if (!isEmail(email.trim())) {
setError('Enter a valid email');
return;
}
setError('');
setDone(true);
}
return (
<div style={{ padding: 16, width: 420 }}>
<h2 style={{ marginTop: 0 }}>meetcode newsletter</h2>
<form onSubmit={submit} style={{ display: 'grid', gap: 10 }}>
<label>
Email
<div>
<input value={email} onChange={(e) => setEmail(e.target.value)} style={{ padding: '10px 12px', borderRadius: 12, border: '1px solid ' + (error ? '#c1121f' : '#bbb'), width: '100%' }} />
</div>
</label>
<button type='submit' style={{ padding: '10px 14px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>Subscribe</button>
</form>
{error ? <div style={{ marginTop: 10, color: '#c1121f' }}>{error}</div> : null}
{done ? <div style={{ marginTop: 10, color: '#0b5' }}>Saved on meetcode.</div> : null}
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!