Skip to content

Commit f89a89b

Browse files
committed
form validator complete
1 parent c6613b6 commit f89a89b

File tree

8 files changed

+10926
-0
lines changed

8 files changed

+10926
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
25+
source

form-validator-react_source/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13+
14+
The page will reload when you make changes.\
15+
You may also see any lint errors in the console.
16+
17+
### `npm test`
18+
19+
Launches the test runner in the interactive watch mode.\
20+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
22+
### `npm run build`
23+
24+
Builds the app for production to the `build` folder.\
25+
It correctly bundles React in production mode and optimizes the build for the best performance.
26+
27+
The build is minified and the filenames include the hashes.\
28+
Your app is ready to be deployed!
29+
30+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31+
32+
### `npm run eject`
33+
34+
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
35+
36+
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37+
38+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39+
40+
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41+
42+
## Learn More
43+
44+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45+
46+
To learn React, check out the [React documentation](https://reactjs.org/).
47+
48+
### Code Splitting
49+
50+
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51+
52+
### Analyzing the Bundle Size
53+
54+
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55+
56+
### Making a Progressive Web App
57+
58+
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59+
60+
### Advanced Configuration
61+
62+
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63+
64+
### Deployment
65+
66+
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67+
68+
### `npm run build` fails to minify
69+
70+
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "form-validator-react",
3+
"version": "0.1.0",
4+
"private": true,
5+
"homepage": "https://paolojr90.github.io/react-apps/form-validator-react",
6+
"dependencies": {
7+
"@testing-library/jest-dom": "^5.16.5",
8+
"@testing-library/react": "^13.4.0",
9+
"@testing-library/user-event": "^13.5.0",
10+
"check-password-strength": "^2.0.7",
11+
"email-validator": "^2.0.4",
12+
"gh-pages": "^5.0.0",
13+
"node-sass": "^9.0.0",
14+
"react": "^18.2.0",
15+
"react-dom": "^18.2.0",
16+
"react-scripts": "5.0.1",
17+
"use-immer": "^0.9.0",
18+
"web-vitals": "^2.1.4"
19+
},
20+
"scripts": {
21+
"start": "react-scripts start",
22+
"build": "react-scripts build",
23+
"test": "react-scripts test",
24+
"eject": "react-scripts eject"
25+
},
26+
"eslintConfig": {
27+
"extends": [
28+
"react-app",
29+
"react-app/jest"
30+
]
31+
},
32+
"browserslist": {
33+
"production": [
34+
">0.2%",
35+
"not dead",
36+
"not op_mini all"
37+
],
38+
"development": [
39+
"last 1 chrome version",
40+
"last 1 firefox version",
41+
"last 1 safari version"
42+
]
43+
}
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<meta name="theme-color" content="#000000" />
7+
<meta
8+
name="description"
9+
content="Web site created using create-react-app"
10+
/>
11+
12+
<title>React Form Validator</title>
13+
</head>
14+
<body>
15+
<noscript>You need to enable JavaScript to run this app.</noscript>
16+
<div id="root"></div>
17+
</body>
18+
</html>
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
import { useImmer } from "use-immer";
2+
import * as EmailValidator from "email-validator";
3+
import { passwordStrength } from "check-password-strength";
4+
import "./app.scss";
5+
6+
const initialState = {
7+
email: "",
8+
password: "",
9+
confirmPassword: "",
10+
showPassword: false,
11+
showInvalidEmail: false,
12+
isPasswordShort: false,
13+
passwordMatch: false,
14+
passwordStrength: {
15+
color: "",
16+
value: "",
17+
},
18+
};
19+
20+
function App() {
21+
const [state, setState] = useImmer(initialState);
22+
23+
const validate =
24+
state.email &&
25+
!state.showInvalidEmail &&
26+
state.password.length > 9 &&
27+
["Strong", "Medium"].includes(state.passwordStrength.value) &&
28+
state.password === state.confirmPassword;
29+
30+
return (
31+
<div id="app">
32+
<form id="my-form" className="shadow">
33+
<h4>Form Validator</h4>
34+
<div className="mb-4">
35+
<label>Email</label>
36+
<input
37+
className="form-control"
38+
type="text"
39+
data-rules="required|digits:5|min:5"
40+
placeholder="Please enter valid email here"
41+
value={state?.email || ""}
42+
onChange={(event) =>
43+
setState((draft) => {
44+
draft.email = event.target.value;
45+
})
46+
}
47+
onBlur={() => {
48+
setState((draft) => {
49+
draft.showInvalidEmail = !EmailValidator.validate(state?.email);
50+
});
51+
}}
52+
/>
53+
{state.showInvalidEmail && (
54+
<p
55+
className="validator-err"
56+
style={{
57+
position: "relative",
58+
color: "red",
59+
fontWeight: "bold",
60+
}}
61+
>
62+
invalid email
63+
</p>
64+
)}
65+
</div>
66+
<div
67+
className="mb-4"
68+
style={{
69+
position: "relative",
70+
}}
71+
>
72+
<label>Password</label>
73+
<input
74+
className="form-control"
75+
type={state.showPassword ? "text" : "password"}
76+
data-rules="required|string|min:5"
77+
placeholder="Test placeholder"
78+
value={state.password || ""}
79+
onChange={(event) => {
80+
setState((draft) => {
81+
draft.password = event.target.value;
82+
if (state.showPassword) {
83+
draft.confirmPassword = event.target.value;
84+
}
85+
if (event.target.value.length > 9) {
86+
const passwordStrengthValue = passwordStrength(
87+
event.target.value
88+
).value;
89+
console.log(passwordStrengthValue);
90+
draft.passwordStrength.value = passwordStrengthValue;
91+
switch (passwordStrengthValue) {
92+
case "Too weak":
93+
draft.passwordStrength.color = "red";
94+
break;
95+
case "Weak":
96+
draft.passwordStrength.color = "orange";
97+
break;
98+
case "Medium":
99+
draft.passwordStrength.color = "blue";
100+
break;
101+
default:
102+
draft.passwordStrength.color = "green";
103+
}
104+
draft.isPasswordShort = false;
105+
} else {
106+
draft.passwordStrength.value = "";
107+
draft.passwordStrength.color = "";
108+
}
109+
});
110+
}}
111+
onBlur={() => {
112+
setState((draft) => {
113+
draft.isPasswordShort = state.password.length < 9;
114+
});
115+
}}
116+
/>
117+
{state.isPasswordShort && (
118+
<p
119+
className="validator-err"
120+
style={{
121+
position: "relative",
122+
color: "red",
123+
fontWeight: "bold",
124+
}}
125+
>
126+
Password must greater than 9 characters
127+
</p>
128+
)}
129+
{state.password && (
130+
<button
131+
style={{
132+
position: "absolute",
133+
color: "black",
134+
top: 26,
135+
right: -7,
136+
left: "auto",
137+
width: 40,
138+
height: 40,
139+
padding: "0px !important",
140+
margin: 0,
141+
fontSize: 8,
142+
border: "none !important",
143+
display: "flex",
144+
justifyContent: "center",
145+
alignItems: "center",
146+
}}
147+
type="button"
148+
onClick={() => {
149+
setState((draft) => {
150+
draft.showPassword = !draft.showPassword;
151+
if (!state.showPassword) {
152+
draft.confirmPassword = state.password;
153+
draft.passwordMatch = true;
154+
} else {
155+
draft.passwordMatch = false;
156+
draft.confirmPassword = "";
157+
}
158+
});
159+
}}
160+
>
161+
show
162+
</button>
163+
)}
164+
</div>
165+
{!state.showPassword && (
166+
<div className="mb-4">
167+
<label>Password Confirm</label>
168+
<input
169+
className="form-control"
170+
type="password"
171+
data-rules="required|string|min:5"
172+
value={state.confirmPassword || ""}
173+
onChange={(event) =>
174+
setState((draft) => {
175+
draft.confirmPassword = event.target.value;
176+
draft.passwordMatch = event.target.value === state.password;
177+
})
178+
}
179+
/>
180+
</div>
181+
)}
182+
{!state.passwordMatch && state.confirmPassword && (
183+
<p
184+
className="validator-err"
185+
style={{
186+
position: "relative",
187+
color: "red",
188+
fontWeight: "bold",
189+
}}
190+
>
191+
Password entries do not match
192+
</p>
193+
)}
194+
195+
{state.passwordStrength.value && (
196+
<div
197+
className="mb-4"
198+
style={{
199+
position: "relative",
200+
color: state.passwordStrength.color,
201+
fontWeight: "bold",
202+
}}
203+
>
204+
{state.passwordStrength.value}
205+
</div>
206+
)}
207+
<ul className="information">
208+
<li> password must be greater than 9 characters</li>
209+
<li> password must contain at least one uppercase letter</li>
210+
<li> password must contain at least one number</li>
211+
<li> password must contain at least one special character</li>
212+
</ul>
213+
<button
214+
disabled={!validate}
215+
style={{ backgroundColor: validate ? "" : "black" }}
216+
onClick={() => {
217+
alert("Form Submitted");
218+
setState(initialState);
219+
}}
220+
type="button"
221+
>
222+
Submit
223+
</button>
224+
</form>
225+
</div>
226+
);
227+
}
228+
229+
export default App;

0 commit comments

Comments
 (0)