Skip to content

Commit 9fa768e

Browse files
committed
Complete Form Validation Project
1 parent 196d80b commit 9fa768e

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed
Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,107 @@
11
import React, { useState } from "react";
22

33
function FormValidation() {
4-
return <div>Form Validation</div>;
4+
const [username, setUsername] = useState("");
5+
const [email, setEmail] = useState("");
6+
const [password, setPassword] = useState("");
7+
const [confirmPassword, setConfirmPassword] = useState("");
8+
9+
const [errorUserName, setErrorUserName] = useState("");
10+
const [errorEmail, setErrorEmail] = useState("");
11+
const [errorPassword, setErrorPassword] = useState("");
12+
const [errorConfirmPassword, setErrorConfirmPassword] = useState("");
13+
14+
const [userColor, setUserColor] = useState("");
15+
const [emailColor, setEmailColor] = useState("");
16+
const [passwordColor, setPasswordColor] = useState("");
17+
const [confirmPasswordColor, setConfirmPasswordColor] = useState("");
18+
19+
function validate(e) {
20+
e.preventDefault();
21+
22+
if (username.length > 8) {
23+
setErrorUserName("");
24+
setUserColor("green");
25+
} else {
26+
setErrorUserName("Username must be 8 letters long.");
27+
setUserColor("red");
28+
}
29+
30+
if (email.includes("@gmail")) {
31+
setErrorEmail("");
32+
setEmailColor("green");
33+
} else {
34+
setEmailColor("red");
35+
setErrorEmail("Email should have @gmail");
36+
}
37+
38+
if (password.length > 8) {
39+
setErrorPassword("");
40+
setPasswordColor("green");
41+
} else {
42+
setErrorPassword("Password should be 8 letters long");
43+
setPasswordColor("red");
44+
}
45+
46+
if (password != "" && password == confirmPassword) {
47+
setErrorConfirmPassword("");
48+
setConfirmPasswordColor("green");
49+
} else {
50+
setErrorConfirmPassword("Passwords didn't matched");
51+
setConfirmPasswordColor("red");
52+
}
53+
}
54+
55+
return (
56+
<>
57+
<div className="card">
58+
<div className="card-image"></div>
59+
60+
<form>
61+
<input
62+
type="text"
63+
placeholder="Name"
64+
style={{ borderColor: userColor }}
65+
value={username}
66+
onChange={(e) => setUsername(e.target.value)}
67+
/>
68+
69+
<p className="error">{errorUserName}</p>
70+
71+
<input
72+
type="text"
73+
placeholder="Email"
74+
style={{ borderColor: emailColor }}
75+
value={email}
76+
onChange={(e) => setEmail(e.target.value)}
77+
/>
78+
<p className="error">{errorEmail}</p>
79+
80+
<input
81+
type="password"
82+
placeholder="Password"
83+
style={{ borderColor: passwordColor }}
84+
value={password}
85+
onChange={(e) => setPassword(e.target.value)}
86+
/>
87+
<p className="error">{errorPassword}</p>
88+
89+
<input
90+
type="password"
91+
placeholder="Confirm Password"
92+
style={{ borderColor: confirmPasswordColor }}
93+
value={confirmPassword}
94+
onChange={(e) => setConfirmPassword(e.target.value)}
95+
/>
96+
<p className="error">{errorConfirmPassword}</p>
97+
98+
<button className="submit-btn" onClick={validate}>
99+
Submit
100+
</button>
101+
</form>
102+
</div>
103+
</>
104+
);
5105
}
6106

7107
export default FormValidation;

0 commit comments

Comments
 (0)