Skip to content

Commit 9e1fcd8

Browse files
committed
add form validator
1 parent ab9ca39 commit 9e1fcd8

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed

59-form validator/index.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.0" />
6+
<link rel="stylesheet" href="style.css" />
7+
<title>Form Validator</title>
8+
</head>
9+
<body>
10+
<div class="container">
11+
<form id="form" class="form">
12+
<h2>Register With Us</h2>
13+
<div class="form-control">
14+
<label for="username">Username</label>
15+
<input type="text" id="username" placeholder="Enter username" />
16+
<small>Error message</small>
17+
</div>
18+
<div class="form-control">
19+
<label for="email">Email</label>
20+
<input type="text" id="email" placeholder="Enter email" />
21+
<small>Error message</small>
22+
</div>
23+
<div class="form-control">
24+
<label for="password">Password</label>
25+
<input type="password" id="password" placeholder="Enter password" />
26+
<small>Error message</small>
27+
</div>
28+
<div class="form-control">
29+
<label for="password2">Confirm Password</label>
30+
<input
31+
type="password"
32+
id="password2"
33+
placeholder="Enter password again"
34+
/>
35+
<small>Error message</small>
36+
</div>
37+
<button>Submit</button>
38+
</form>
39+
</div>
40+
<script src="script.js"></script>
41+
</body>
42+
</html>

59-form validator/script.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const form = document.getElementById("form");
2+
const username = document.getElementById("username");
3+
const email = document.getElementById("email");
4+
const password = document.getElementById("password");
5+
const password2 = document.getElementById("password2");
6+
7+
function showError(input, message) {
8+
const formControl = input.parentElement;
9+
formControl.className = "form-control error";
10+
const small = formControl.querySelector("small");
11+
small.innerText = message;
12+
}
13+
14+
function showSuccess(input, message) {
15+
const formControl = input.parentElement;
16+
formControl.className = "form-control success";
17+
}
18+
19+
function getFieldName(input) {
20+
return input.id.charAt(0).toUpperCase() + input.id.slice(1);
21+
}
22+
23+
function checkRequired(inputs) {
24+
inputs.forEach((input) => {
25+
if (input.value.trim() === "") {
26+
showError(input, `${getFieldName(input)} is required`);
27+
} else {
28+
showSuccess(input);
29+
}
30+
});
31+
}
32+
33+
function checkLength(input, min, max) {
34+
if (input.value.length < min) {
35+
showError(
36+
input,
37+
`${getFieldName(input)} must be at least ${min} characters`
38+
);
39+
} else if (input.value.length > max) {
40+
showError(
41+
input,
42+
`${getFieldName(input)} must be less than ${max} characters`
43+
);
44+
} else {
45+
showSuccess(input);
46+
}
47+
}
48+
49+
function checkEmail(input) {
50+
// Reference: https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript
51+
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
52+
if (re.test(String(input.value.trim()).toLowerCase())) showSuccess(input);
53+
else showError(input, `${getFieldName(input)} is not valid`);
54+
}
55+
56+
function checkPasswordMatch(input1, input2) {
57+
if (input1.value !== input2.value) {
58+
showError(input2, "Passwords do not match");
59+
}
60+
}
61+
62+
form.addEventListener("submit", (e) => {
63+
e.preventDefault();
64+
checkRequired([username, email, password, password2]);
65+
checkLength(username, 3, 15);
66+
checkLength(password, 6, 25);
67+
checkEmail(email);
68+
checkPasswordMatch(password, password2);
69+
});

59-form validator/style.css

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
@import url("https://fonts.googleapis.com/css?family=Open+Sans&display=swap");
2+
3+
:root {
4+
--main-color: #3498db;
5+
--success-color: #2ecc71;
6+
--error-color: #e74c3c;
7+
}
8+
9+
* {
10+
box-sizing: border-box;
11+
}
12+
13+
body {
14+
background-color: #f9fafb;
15+
font-family: "Open Sans", sans-serif;
16+
display: flex;
17+
align-items: center;
18+
justify-content: center;
19+
min-height: 100vh;
20+
margin: 0;
21+
}
22+
23+
.container {
24+
background-color: #fff;
25+
border-radius: 5px;
26+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
27+
width: 400px;
28+
}
29+
30+
.form {
31+
padding: 30px 40px;
32+
}
33+
34+
h2 {
35+
text-align: center;
36+
margin: 0 0 20px;
37+
}
38+
39+
.form-control {
40+
margin-bottom: 10px;
41+
padding-bottom: 20px;
42+
position: relative;
43+
}
44+
45+
.form-control label {
46+
color: #777;
47+
display: block;
48+
margin-bottom: 5px;
49+
}
50+
51+
.form-control input {
52+
border: 2px solid #f0f0f0;
53+
border-radius: 4px;
54+
display: block;
55+
width: 100%;
56+
padding: 10px;
57+
font-size: 14px;
58+
}
59+
60+
.form-control input:focus {
61+
outline: 0;
62+
border-color: #777;
63+
}
64+
65+
.form-control.success input {
66+
border-color: var(--success-color);
67+
}
68+
69+
.form-control.error input {
70+
border-color: var(--error-color);
71+
}
72+
73+
.form-control small {
74+
color: var(--error-color);
75+
position: absolute;
76+
bottom: 0;
77+
left: 0;
78+
visibility: hidden;
79+
}
80+
81+
.form-control.error small {
82+
visibility: visible;
83+
}
84+
85+
.form button {
86+
cursor: pointer;
87+
background-color: var(--main-color);
88+
border: 2px solid var(--main-color);
89+
border-radius: 4px;
90+
color: #fff;
91+
display: block;
92+
font-size: 16px;
93+
padding: 10px;
94+
margin-top: 20px;
95+
width: 100%;
96+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
| 56 | [Image Comparison Slider](https://github.com/solygambas/html-css-fifty-projects/tree/master/56-image%20comparison%20slider) | [Live Demo](https://codepen.io/solygambas/full/RwoMLYW) |
6565
| 57 | [Parallax Background with SVG](https://github.com/solygambas/html-css-fifty-projects/tree/master/57-parallax%20background%20svg) | [Live Demo](https://codepen.io/solygambas/full/vYyjjbz) |
6666
| 58 | [3D Product Card](https://github.com/solygambas/html-css-fifty-projects/tree/master/58-3D%20product%20card) | [Live Demo](https://codepen.io/solygambas/full/wvoXWPq) |
67+
| 59 | [Form Validator](https://github.com/solygambas/html-css-fifty-projects/tree/master/59-form%20validator) | [Live Demo](https://codepen.io/solygambas/full/MWbPJjb) |
6768

6869
Mainly based on 2 courses by Brad Traversy (2020):
6970

0 commit comments

Comments
 (0)