7 Steps You Should Consider Before Starting Your Project
1. Defining Requirements: Identify the form fields and validation rules.
2. Planning state management: Decide how to manage the form state using useState
3. Designing event handlers: Create functions to update the form state on user input.
4. Implementing validation logic: Write validation rules and update the error state accordingly.
5. Handling form submission: Validate form data and display appropriate messages.
6. Providing user feedback: Show success or error messages to the user.
7. Testing thoroughly: Ensure the form works correctly in various scenarios.
Understanding Form Validation:
- Passwords must be at least 8 characters long.
- They must include uppercase and lowercase letters.
- At least one digit and special characters are necessary.
Launching Your React Adventure: Getting Started
npx create-react-app form-validation
import React, { useState } from 'react';
import './FormComponent.css';
const FormComponent = () => {
const [formData, setFormData] = useState({
firstName: '',
lastName: '',
email: '',
mobileNumber: '',
password: '',
confirmPassword: '',
});
const [errors, setErrors] = useState({});
const [submitted, setSubmitted] = useState(false);
const handleChange = (e) => {
const { name, value } = e.target;
setFormData({
...formData,
[name]: value,
});
};
const handleSubmit = (e) => {
e.preventDefault();
const newErrors = {};
if (!formData.firstName) {
newErrors.firstName = 'First name is required';
}
if (!formData.lastName) {
newErrors.lastName = 'Last name is required';
}
if (!formData.email) {
newErrors.email = 'Email is required';
} else if (!/\S+@\S+\.\S+/.test(formData.email)) {
newErrors.email = 'Email is invalid';
}
if (!formData.mobileNumber) {
newErrors.mobileNumber = 'Mobile number is required';
} else if (!/^\d{10}$/.test(formData.mobileNumber)) {
newErrors.mobileNumber = 'Mobile number is invalid';
}
if (!formData.password) {
newErrors.password = 'Password is required';
} else if (formData.password.length < 8 || !/\d/.test(formData.password) || !/[a-zA-Z]/.test(formData.password)) {
newErrors.password = 'Password must be at least 8 characters long and contain at least one letter and one digit';
}
if (!formData.confirmPassword) {
newErrors.confirmPassword = 'Confirmation password is required';
} else if (formData.confirmPassword !== formData.password) {
newErrors.confirmPassword = 'Passwords do not match';
}
setErrors(newErrors);
if (Object.keys(newErrors).length === 0) {
console.log(formData);
setFormData({
firstName: '',
lastName: '',
email: '',
mobileNumber: '',
password: '',
confirmPassword: '',
});
setSubmitted(true);
setTimeout(() => {
setSubmitted(false);
}, 3000);
}
};
return (
<>
<h1>Form Validation</h1>
<div className="form-container">
<form onSubmit={handleSubmit}>
<div className="form-group">
<label>First Name:</label>
<input
type="text"
name="firstName"
value={formData.firstName}
onChange={handleChange}
/>
{errors.firstName && <span className="error-message">{errors.firstName}</span>}
</div>
<div className="form-group">
<label>Last Name:</label>
<input
type="text"
name="lastName"
value={formData.lastName}
onChange={handleChange}
/>
{errors.lastName && <span className="error-message">{errors.lastName}</span>}
</div>
<div className="form-group">
<label>Email:</label>
<input
type="email"
name="email"
value={formData.email}
onChange={handleChange}
/>
{errors.email && <span className="error-message">{errors.email}</span>}
</div>
<div className="form-group">
<label>Mobile Number:</label>
<input
type="text"
name="mobileNumber"
value={formData.mobileNumber}
onChange={handleChange}
/>
{errors.mobileNumber && <span className="error-message">{errors.mobileNumber}</span>}
</div>
<div className="form-group">
<label>Password:</label>
<input
type="password"
name="password"
value={formData.password}
onChange={handleChange}
/>
{errors.password && <span className="error-message">{errors.password}</span>}
</div>
<div className="form-group">
<label>Confirm Password:</label>
<input
type="password"
name="confirmPassword"
value={formData.confirmPassword}
onChange={handleChange}
/>
{errors.confirmPassword && <span className="error-message">{errors.confirmPassword}</span>}
</div>
<div className="btn">
<button type="submit">Submit</button></div>
{submitted && <div className="success-message">Form has been submitted</div>}
</form>
</div>
</>
);
};
export default FormComponent;
1. import React, { useState } from 'react'
2. import './FormComponent.css'
.form-container {
width: 25%;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
font-family: sans-serif;
}
h1 {
font-family: sans-serif;
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
input {
width: 96%;
font-size: 16px;
padding: 8px;
border: 1px solid #ccc;
border-radius: 5px;
}
.error-message {
color: red;
font-size: 14px;
}
.btn {
display: flex;
justify-content: center;
}
button {
padding: 10px 20px;
border-radius: 5px;
border: none;
color: white;
background-color: black;
cursor: pointer;
}
.success-message {
margin: 0 auto;
text-align: center;
font-size: 20px;
color: green;
border-radius: 5px;
padding: 12px;
margin-bottom: 10px;
}
3. const FormComponent = () => {..}
4. const [formData, setFormData] = useState({..})
5. const [errors, setErrors] = useState({...});
6. const [submitted, setSubmitted] = useState(false);
7. const handleChange = (e) => {...}
8. const handleSubmit = (e) => {...}
9. return (...)
10. {submitted && <div className="success-message">Form has been submitted</div>}
Creating App.js:
import FormComponent from './FormComponent'
function App() {
return (
<>
<FormComponent />
</>
);
}
export default App;