DB로 하는 것이 옳습니다. 그러나 DB를 배우기 전에 사이트에 틀이라도 회원가입을 구현하고싶어서 localStorage를 사용해봤습니다. 실전에서는 회원가입은 localStorage로 진행하면 절대 안됩니다. 민감한 정보들을 다루기 때문입니다. 또한 localStorage는 개발자도구에서 저장한 데이터를 볼 수 있습니다.
#HTML
<form class="singUp-form">
<input type="email" id="email" placeholder="Email" required />
<input type="password" id="password" placeholder="password" required />
<input type="submit" id="singUp-form" value="SingUp" />
</form>
추가로 name하고 싶다면 type, id, plascholder 값만 다르게 지정하시면 됩니다. name의 type은 "text"로 하시면 됩니다.
# JS
const singUpForm = document.querySelector('.singUp-form');
const email = document.getElementById('email');
const password = document.getElementById('password');
function singUpData(evn) {
evn.preventDefault(); // 이벤트 _ form 기본 동작인 새로고침 막기
const userEmail = email.value;
const userPassword = password.value;
singUpShow(userEmail, userPassword);
};
function singUpShow(userEmail, userPassword){
localStorage.setItem("userEmail", userEmail);
localStorage.setItem("userPassword", userPassword);
alert(`${userEmail}님, 반갑습니다`);
};
singUpForm.addEventListener("submit", singUpData);
input 값을 상수에 넣고 그 값들만 singUpShow 함수에 넣었습니다. singUpShow 인자 값들은 localStorage()에 저장했습니다.
localStorage에 저장하기위해 setItem()메소드를 사용했습니다.
localStorage.setItem("localStorage Key", 저장할 값);
브라우저에서 localStorage 보는 법은 chrome > ctrl + shift + I 또는 오른쪽 상단에서 도구 더보기의 개발자도구 > Application에서 Local Storage 클릭하면 볼 수 있습니다.
정규표현식은 저번에 올린 정규표현식을 사용한 회원가입 게시글을 참고하시면 좋을 듯 합니다.
* 참고 사이트 *
input type에 대해서 더 알고싶다면 아래 사이트 참고해주세요.
HTML Input 요소의 타입들(types)_jun.hansung
http://jun.hansung.ac.kr/CWP/htmls/HTML%20Input%20Types.html
'Java script' 카테고리의 다른 글
[js/nodejs] 회원 가입 시 email__ 정규표현식 이용하여 비교하기 (0) | 2021.12.03 |
---|---|
[js] console.time()과 console.timeEnd(), 작업시간을 알고 싶을 때 (0) | 2021.11.24 |
[js] setTimeout으로 일정 시간마다 새로고침하기& 새로고침 버튼 만들기 (0) | 2021.11.12 |
[js] if문과 유효성검사__정규표현식을 사용하여 회원가입 구현 (0) | 2021.11.11 |
[js] if문으로 로그인 구현 (0) | 2021.11.10 |