![[Tistory] 비밀번호 동일체크(샘플링)](https://image.inblog.dev?url=https%3A%2F%2Finblog.ai%2Fapi%2Fog%3Ftitle%3D%255BTistory%255D%2520%25EB%25B9%2584%25EB%25B0%2580%25EB%25B2%2588%25ED%2598%25B8%2520%25EB%258F%2599%25EC%259D%25BC%25EC%25B2%25B4%25ED%2581%25AC%28%25EC%2583%2598%25ED%2594%258C%25EB%25A7%2581%29%26logoUrl%3Dhttps%253A%252F%252Finblog.ai%252Finblog_logo.png%26blogTitle%3DCoding_study&w=2048&q=75)
- key event
비밀번호
비밀번호확인칸 (글자 적을때 이벤트 발생)
뷰 래퍼런스 느낌
비번을 입력했을때 틀리면 틀렸고, 일치하면 일치한다는 메세지를 넣을것임
이런 너낌으로~~

1. 샘플코드 만들기 - VScode go!!

2. input 이벤트 추가
비밀번호가 일치하면 일치하다는 글자가 떠야하고, 일치하지 않으면 일치하지 않다는 글자가 떠야함


일단, 처음에는 해당 텍스트 div가 보이지 않게 해줌
input 이벤트가 발생하면 remove와 add 해줄 것 임
선택자끼리 직접 비교 x
각자 val값을 잡아와서 비교해야할 것 같음

2번째 비밀번호 확인 input에 값을 입력하면 잡아오게 만들었음
<script> $("#same-password").on("input", function() { // const password = $("#password").val(); // const confirmPassword = $(this).val(); let password = $("#password").val(); // 비밀번호 입력값 console.log("패스워드 = " + password); let confirmPassword = $(this).val(); // 비밀번호 확인 입력값 console.log("확인용 = " + confirmPassword); if (password === confirmPassword) { $("#successText").show(); $("#errorText").hide(); $("#same-password").removeClass("error").addClass("correct"); } else { $("#successText").hide(); $("#errorText").show(); $("#same-password").removeClass("correct").addClass("error"); } }); </script>
- const를 사용할까 했으나 비밀번호와 비밀번호 확인 입력 필드의 값을 매번 비교하여 일치 여부를 판단하는 코드에서
let
을 사용하기로 함(추후 눈깜빡이는 뷰로 바꿀 것이므로)
3. 비번 다 지웠을때 Text 보이지 않게


4. 비번 일치하면 버튼, 텍스트 색상 변경

6. 비밀번호 동일 체크 부분 코드 수정 - 전체코드
<input id="same-password" class="my_auth_form_box_input" type="password" name="" placeholder="비밀번호 확인" maxlength="20" required /> <div id="errorText" style="color: red">비밀번호가 일치하지 않습니다.</div> <div id="successText" style="color: blue">비밀번호가 일치합니다.</div> <!-- 비밀번호 동일 체크 --> <script> $("#same-password").on("input", function() { let password = $("#password").val(); // 비밀번호 입력값 console.log("패스워드 = " + password); let confirmPassword = $("#same-password").val(); // 비밀번호 확인 입력값 console.log("확인용 = " + confirmPassword); if (password === confirmPassword) { $("#successText").show(); $("#errorText").hide(); $("#same-password").removeClass("error").addClass("correct"); $("#joinBtn").css({ "background-color": "rgb(255, 85, 68)", "color": "rgba(255,255,255,.8)" }); } else if (confirmPassword === "") { $("#successText").hide(); $("#errorText").hide(); } else { $("#successText").hide(); $("#errorText").show(); $("#same-password").removeClass("correct").addClass("error"); $("#joinBtn").css({ "background-color": "", "color": "" }); } }); </script> <!-- 비밀번호 동일 체크 끝-->
이제 적용하러 가볼까!!!
Share article