유데미에서 Brad Traversy의 20 Web Projects With Vanilla JavaScript
를 하나씩 직접 코딩해보면서 정리하기 위한 글이다.
데모 페이지
Form Validator (vanillawebprojects.com)
코드 링크
github.com/rshak8912/20-Web-Projects
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Valiator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<form action="" id="form" class="form">
<h2>등록 폼</h2>
<div class="form-control">
<label for="username">이름</label>
<input type="text" id="username" placeholder="이름을 입력하세요">
<small>Error message</small>
</div>
<div class="form-control">
<label for="email">이메일</label>
<input type="text" id="email" placeholder="이메일을 입력하세요">
<small>Error message</small>
</div>
<div class="form-control">
<label for="password">비밀번호</label>
<input type="password" id="password" placeholder="암호를 입력하세요">
<small>Error message</small>
</div>
<div class="form-control">
<label for="password2">비밀번호 확인</label>
<input type="password" id="password2" placeholder="암호를 다시 한번 입력하세요">
<small>Error message</small>
</div>
<button type="submit">제출</button>
</form>
</div>
<script src="app.js"></script>
</body>
</html>
style.css
@import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');
:root {
--success-color:#2ecc71;
--error-color:#e74c3c;
}
* {
box-sizing: border-box;
}
body {
background-color: #f9fafb;
font-family: 'Open Sans', sans-serif;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
}
.container {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
width: 400px;
}
h2 {
text-align: center;
margin: 0 0 20px;
}
.form {
padding: 30px 40px;
}
.form-control {
margin-bottom: 10px;
padding-bottom: 20px;
position: relative;
}
.form-control label {
color: #777;
display: block;
margin-bottom: 5px;
}
.form-control input {
border: 2px solid #f0f0f0;
border-radius: 4px;
display: block;
width: 100%;
padding: 10px;
font-size: 14px;
}
.form-control input:focus {
outline: 0;
border-color: #777;
}
.form-control.success input {
border-color:var(--success-color);
}
.form-control.error input {
border-color: var(--error-color);
}
.form-control small {
color:var(--error-color);
position: absolute;
bottom: 0;
left: 0;
visibility: hidden;
}
.form-control.error small {
visibility: visible;
}
.form button {
cursor: pointer;
background-color: #3498db;
border: 2px solid #3498db;
border-radius: 4px;
color:#fff;
display: block;
font-size: 16px;
padding: 10px;
margin-top: 20px;
width: 100px;
}
app.js
const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password = document.getElementById('password');
const password2 = document.getElementById('password2');
function showError(input, message) {
const formControl = input.parentElement;
formControl.className= 'form-control error';
const small = formControl.querySelector('small');
small.innerText = message;
}
// Show success outine
function showSuccess(input) {
const formControl = input.parentElement;
formControl.className= 'form-control success';
}
//Check email is valid
function checkEmail(input) {
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,}))$/;
if (re.test(input.value.trim())) {
showSuccess(input);
} else {
showError(input, 'Email is not valid');
}
};
// Check required fields
function checkRequired(inputArr) {
inputArr.forEach(function(input) {
if (input.value.trim() === '') {
showError(input, `${getFieldName(input)} is required`);
} else {
showSuccess(input);
}
});
}
function checkLength(input, min, max) {
if (input.value.length < min) {
showError(input, `${getFieldName(input)} must be at least ${min} characters`);
} else if (input.value.length > max) {
showError(input, `${getFieldName(input)} must be less than ${max} characters`);
} else {
showSuccess;
}
}
// Check passwords match
function checkPasswordsMatch(input1, input2) {
if (input1.value !== input2.value) {
showError(input2, 'Passwords do not match');
}
}
// Get fieldname
function getFieldName(input) {
return input.id.charAt(0).toUpperCase() + input.id.slice(1);
}
// event listeners
form.addEventListener('submit', function(e) {
e.preventDefault();
checkRequired([username, email, password, password2]);
checkLength(username, 3, 15);
checkLength(password, 6, 20);
checkEmail(email);
checkPasswordsMatch(password, password2);
}) ;
강의를 통해 배운점(생각 정리)
- HTML의 form 의 구조(form-control class)가 부트스트랩을 따라한 것 같다.
- CSS의 폰트를 이전에는 html 파일에 link태그를 사용하는 방법만 알고 있었는데, import를 사용해 본 점
- css의 변수(var()) 사용을 익힐 수 있었다.
- 자바스크립트 이벤트 함수에서 리팩토링 과정과 클린코드 작성법(check 메서드).
'HTML&CSS&Javascript' 카테고리의 다른 글
[Udemy]20 Web Projects With Vanilla JavaScript -Project#5 (0) | 2020.12.10 |
---|---|
[Udemy]20 Web Projects With Vanilla JavaScript -Project#4 (0) | 2020.12.09 |
[Udemy]20 Web Projects With Vanilla JavaScript -Project#3 (0) | 2020.12.08 |
[Udemy]20 Web Projects With Vanilla JavaScript -Project#2 (0) | 2020.12.08 |
Hoisting(호이스팅) (0) | 2020.12.02 |