์๊ตฌ์ฌํญ
id : ๋ฌธ์(์์ด), ์ซ์๋ง, ์ค๋ณต์ฒดํฌ, "test"
pw : ๋น๋ฐ๋ฒํธํ์ธ(๊ฐ์ ์ ์ฒญ๋ฒํผ ํด๋ฆญ์)
์ด๋ฆ : 5๊ธ์(maxlength x)-script ๋ฌธ์(ํ๊ธ,์์ด) 5๊ธ์ ๋์ด๊ฐ ์ ๋ฐ๋ก ์ฒดํฌ
์ด๋ฉ์ผ : ์ ํจ์ฑ(๊ฐ์ ์ ์ฒญ๋ฒํผ ํด๋ฆญ์)
ํด๋ํฐ : ์ซ์๋ง 4์๋ฆฌ -> 3๋ฒ์งธ๋ก 4๊ฐ ์ ๋ ฅํ๋ฉด 3๋ฒ์งธ ์ฐฝ์ผ๋ก ์ปค์์ด๋
์ฃผ๋ฏผ๋ฒํธ : ์ซ์๋ง ์ฃผ๋ฏผ๋ฒํธ ์ ํจ์ฑ ๋ท์๋ฆฌ ๋งจ์ฒซ์๋ฆฌ๋ง ์ซ์ 1****** (๊ฐ์ ์ ์ฒญํด๋ฆญ์)
์์ ํ๋ฉด
- ๊ฐ์ ๊ธฐ๋ฅ์ JavaScript์ JQuery ๋๊ฐ์ง๋ก ๊ตฌํํด๋ด
JavaScript
(์๊ตฌ ์ฌํญ์ ์๋ชป ํด์ํ ์ฝ๋๊ฐ ์์ ์ ์์. ๋ค์์ JQuery์์๋ ๋ณด์.)
/* ======================================================= */
/*
* ๊ฐ์
์ ์ฒญ ๋ฒํผ
* */
const regi_btn = document.getElementById('regi_btn'); //๊ฐ์
์ ์ฒญ ๋ฒํผ
regi_btn.addEventListener('click', function () {
if (!idUnqPass()) return;
if (!passwordValid()) return;
if (!emailValid()) return;
if (!validJuminNumber(jumin1.value)) return;
if (!validJuminNumber(originalJumin2)) return;
alert('ํ์๊ฐ์
์กฐ๊ฑด ์ถฉ์กฑ');
});
//id์ค๋ณต์ฌ๋ถ ํ์ธ ํ๋์ง
let idUnqChk = false;
function idUnqPass() {
if (!idUnqChk) {
alert('ID ์ค๋ณต์ฒดํฌ๋ฅผ ํด์ฃผ์ธ์');
return false;
} else {
return true;
}
}
/*
*id : ๋ฌธ์(์์ด), ์ซ์๋ง, ์ค๋ณต์ฒดํฌ, "test"
* */
const idChkBtn = document.getElementById('idChkBtn');
idChkBtn.addEventListener('click', function () {
//ID ์ ํจ์ฑ ๊ฒ์ฌ
idUnqId();
}
)
// input ์์ ๊ฐ์ ธ์ค๊ธฐ
const idInput = document.getElementById('id');
// ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ถ๊ฐ(์
๋ ฅ๋ง๋ค)
idInput.addEventListener('keyup', function () {
const inputValue = idInput.value;
// ์ ํจ์ฑ ๊ฒ์ฌ true๋ฉด ์๋ด๊ธ์ ์จ๊ธฐ๊ธฐ
if (isValidId(inputValue)) {
hideComment();
}
});
/*
* ์์ด๋ text ๋ฐ๊ธฐ
* ์ ํจํ๊ฐ t/f
* f๋ฉด ์๋ด๊ธ์ ์ ๋ฌ
* */
function isValidId(text) {
let idText = text;
if (/^[0-9]+$/.test(idText)) {
//์์ด๋ฅผ ํฌํจํด์ผ ํ๋ค๋ ์๋ด
createComment('includeEN');
return false;
} else {
if (/^[a-zA-Z0-9]+$/.test(idText)) {
//์์ด+์ซ์์ธ์ง ํ๋ณ
return true;
} else {
//์์ด+์ซ์ ์กฐํฉ์ด์ด์ผ ํ๋ค๋ ์๋ด
createComment('ENNUM');
return false;
}
}
}
//์๋ด ํ๋ ํ
์คํธ ๋ณ๊ฒฝํ๋ ํจ์
function createComment(commentType) {
let commentElement = document.getElementById('idValidInfo');
if (commentType === 'includeEN') {
commentElement.innerHTML = 'ID์ ์์ด๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค ';
} else if (commentType === 'ENNUM') {
commentElement.innerHTML = 'ID๋ฅผ ์์ด,์ซ์ ์กฐํฉ์ผ๋ก ์์ฑํด์ฃผ์ธ์';
}
commentElement.removeAttribute('hidden');
}
// ID ์ ํจ์๋ด ์จ๊ธฐ๊ธฐ
function hideComment() {
let commentElement = document.getElementById('idValidInfo');
commentElement.setAttribute('hidden', 'true');
}
/*์ผ๋จ ์์๋ก ๋ง๋ ๋ฐฐ์ด์์ ๊ฐ์ ์์ด๋ ์์ผ๋ฉด ์ค๋ณต์ด๋ผ๊ณ alert*/
function idUnqId() {
let idArr = ['aaa', 'bbb', 'ccc', 'apple123', 'test'];
if (idArr.includes(idInput.value)) {
alert("์ด๋ฏธ ์กด์ฌํ๋ ์์ด๋ ์
๋๋ค.");
//์์ฑ ์์ด๋ ์ญ์ , ํฌ์ปค์ค ์ฎ๊ธฐ๊ธฐ
console.log('idInput.value: ' + idInput.value);
idInput.value = "";
idInput.focus();
} else {
if (isValidId(idInput.value)) {
alert("์ฌ์ฉ ๊ฐ๋ฅํ ์์ด๋ ์
๋๋ค");
idUnqChk = true;
idInput.disabled = true;
document.getElementById('pass').focus();
}
}
}
/* ======================================================= */
/*
* pw : ๋น๋ฐ๋ฒํธํ์ธ(๊ฐ์
์ ์ฒญ๋ฒํผ ํด๋ฆญ์)
* */
function passwordValid() {
const pass = document.getElementById('pass').value;
const pass2 = document.getElementById('pass2').value;
//๋น๋ฐ๋ฒํธ ๊ฐ์์ง ๊ฒ์ฌ
if (pass === null || pass2 === null) {
return false;
} else if (pass !== pass2) {
alert("๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค");
return false;
} else {
return true;
}
}
/* ======================================================= */
/*
* ์ด๋ฆ : 5๊ธ์(maxlength x)-script
* ๋ฌธ์(ํ๊ธ,์์ด)
* 5๊ธ์ ๋์ด๊ฐ ์ ๋ฐ๋ก ์ฒดํฌ
*/
function fncNameKeyUp(element) {
const nameValue = element.value;
const regex = /^[A-Za-z๊ฐ-ํฃ]+$/;
// 5๊ธ์? -> ๋ฌธ์?
if (nameValue.length > 5) {
element.value = nameValue.substring(0, 5);
return false;
} else {
if (!regex.test(nameValue)) {
// ์
๋ ฅํ๋์ ์ค๋ฅ ์คํ์ผ ์ถ๊ฐํ๊ธฐ
element.classList.add("input-error");
return false;
} else {
// ๊ด์ฐฎ์ผ๋ฉด ์ค๋ฅ ์คํ์ผ ์ ๊ฑฐ
element.classList.remove("input-error");
return true;
}
}
}
/* ======================================================= */
/*
* ์ด๋ฉ์ผ : ์ ํจ์ฑ(๊ฐ์
์ ์ฒญ๋ฒํผ ํด๋ฆญ์)
* ์์ ์์ด, ์ซ์๋ง ๊ฐ๋ฅํจ, @๋ท์ชฝ์ ์ ํด์ง ์ด๋ฉ์ผ ๋ด์์๋ง ๊ฐ๋ฅํจ
* */
function emailValid() {
const email1Element = document.getElementById('email1');
const email1 = document.getElementById('email1').value;
const email2 = document.getElementById('email2').value;
const emailArr = ["gmail.com", "naver.com", "daum.net", "nate.com", "hanmail.net", "korea.com"];
const emailRegex = /^[a-zA-Z0-9._-]+$/;
if (email1 === null || email2 === null) {
alert('email์ ์
๋ ฅํด์ฃผ์ธ์')
email1Element.focus();
return false;
} else if (!emailRegex.test(email1) || !emailArr.includes(email2)) {
alert('email ์ฃผ์๋ฅผ ํ์ธํด์ฃผ์ธ์');
email1Element.focus();
return false;
} else {
return true;
}
}
/* ======================================================= */
/*
* ํด๋ํฐ :
* ์ซ์๋ง 4์๋ฆฌ -> 3๋ฒ์งธ๋ก
* 4๊ฐ ์
๋ ฅํ๋ฉด 3๋ฒ์งธ ์ฐฝ์ผ๋ก ์ปค์์ด๋
* */
document.getElementById('ph2')
.addEventListener('input', function () {
focusMove(this, 4, 'ph3')
});
/*์ฃผ๋ฏผ๋ฒํธ๋ ์ปค์์ด๋*/
document.getElementById('jumin1')
.addEventListener('input', function () {
focusMove(this, 6, 'jumin2')
});
/*์ฃผ๋ฏผ๋ฒํธ๋ ์ปค์์ด๋*/
document.getElementById('jumin2')
.addEventListener('input', function () {
focusMove(this, 7, 'address')
});
/*์ปค์์ด๋ ๊ณตํต๊ธฐ๋ฅ ํจ์ ์ถ์ถ*/
function focusMove(element, maxLength, nextInputId) {
if (element.value.length === maxLength) {
document.getElementById(nextInputId).focus();
}
}
/* ======================================================= */
/*
* ์ฃผ๋ฏผ๋ฒํธ : ์ซ์๋ง ์ฃผ๋ฏผ๋ฒํธ ์ ํจ์ฑ
* ๋ท์๋ฆฌ ๋งจ์ฒซ์๋ฆฌ๋ง ์ซ์ 1******
* */
//์๋ณธ๊ฐ ์ ์ฅ
let originalJumin2 = '';
const numberRegex = /^[0-9]+$/;
const jumin1 = document.getElementById('jumin1');
//์ซ์๋ง
function validJuminNumber(juminNum) {
if (juminNum === "") {
alert('์ฃผ๋ฏผ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์.');
return false;
} else if (!numberRegex.test(juminNum)) {
console.log("!numberRegex.test(juminNum)"+!numberRegex.test(juminNum));
console.log("jumin1Value+ "+jumin1Value);
alert('์ฃผ๋ฏผ๋ฒํธ๋ฅผ ์ ํํ ์
๋ ฅํด์ฃผ์ธ์');
return false;
} else {
return true;
}
}
document.getElementById('jumin2').addEventListener('input', function () {
const inputValue = this.value;
// ์
๋ ฅ๊ฐ์ด ๋ ๊ธด ๊ฒฝ์ฐ: ์๋ณธ์ ๋ง์ง๋ง ๊ธ์๋ฅผ ์
๋ฐ์ดํธ
if (inputValue.length > originalJumin2.length) {
originalJumin2 += inputValue[inputValue.length - 1];
}
// ์
๋ ฅ๊ฐ์ด ๋ ์งง์ ๊ฒฝ์ฐ: ์๋ณธ์ ๋ง์ง๋ง ๊ธ์๋ฅผ ์ญ์
if (inputValue.length < originalJumin2.length) {
originalJumin2 = originalJumin2.substring(0, inputValue.length);
}
// ํ๋ฉด์๋ ๋ง์คํน๋ ๊ฐ ํ์
this.value = maskingNumber(inputValue);
console.log("๋ง์คํน ์๋ฃ");
console.log("originalJumin2: " + originalJumin2);
});
//jumin2 ์ผ๋ถ *ํ์
function maskingNumber(number) {
if (number.length > 1) {
return number.substring(0, 1) + "*".repeat(number.length - 1);
} else {
return number;
}
}
JQuery
$(function () {
//์์ด๋ ์ค๋ณต์ฒดํฌ
$("#idChkBtn").click(function () {
const idElement = $("#id");
let idVal = idElement.val();
if (idVal == '') {
alert("id๋ฅผ ์
๋ ฅํ์ธ์");
idElement.focus();
} else if (idVal == 'test') {
alert("์ด๋ฏธ ์กด์ฌํ๋ id์
๋๋ค");
idElement.val("").focus();
} else {
alert("์ฌ์ฉ ๊ฐ๋ฅํ id์
๋๋ค");
idElement.attr("disabled", "disabled");
$("#idUptBtn").html("<input type=button value=์์ ํ๊ธฐ>");
$("#pass").focus();
}
});
//์์ด๋ ์์
$("#idUptBtn").click(function () {
const idElement = $("#id");
//๋นํ์ฑํ ํ์ด์ฃผ๊ณ
idElement.removeAttr("disabled");
//ํฌ์ปค์ค ์ฎ๊ฒจ์ฃผ๊ณ
idElement.focus();
//์์ ๋ฒํผ ๋ค์ ์์ ๊ณ
$("#idUptBtn").empty();
});
//์์ด๋ ๋ฌธ์(์์ด), ์ซ์๋ง (์ ๊ทํํ์?)
const textNumberRegex = /^[a-zA-Z0-9]+$/;
const textRegex = /^[a-zA-Z๊ฐ-ํฃ]+$/;
const numberRegex = /^[0-9]+$/;
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
$("#id").on("input", function () {
const idVal = $(this).val();
console.log("idVal " + idVal);
if (!textNumberRegex.test(idVal)) {
$("li#idValidInfo").text('id๋ฅผ ์์ด์ ์ซ์๋ก ์
๋ ฅํด์ฃผ์ธ์').removeAttr("hidden");
} else {
$("li#idValidInfo").attr("hidden", "hidden");
}
});
//๋น๋ฐ๋ฒํธ ์ผ์น ํ์ธ
function passwordCheck() {
const pw = $("#pass").val();
const pw2 = $("#pass2").val();
if ((pw != null) || (pw2 != null)) {
alert('๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์');
$("#pass").focus();
return false;
} else if (pw != pw2) {
alert('๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค.');
$("#pass").focus();
return false;
} else {
return true;
}
}
//์ด๋ฆ ๊ธ์์ ์ฒดํฌ - ๋์ด๊ฐ๋ฉด ํ
๋๋ฆฌ ๋นจ๊ฐ๊ฒ(ํด๋์ค ์ถ๊ฐ), ์๋์ ์๋ด๋ฌธ๊ตฌ
$("#name").on("keyup", function () {
if ($(this).val().length > 5) {
alert("5๊ธ์ ์ดํ๋ง");
$(this).val($(this).val().slice(0, 5));
}
});
//ํฌ์ปค์ค ์๋์ผ๋ก ์ฎ๊ฒจ๊ฐ๋ ํจ์
function moveFocus(element, textSize, nextElement) {
if ($(element).val().length >= textSize) {
$(nextElement).focus();
}
}
//์ด๋ฉ์ผ ์ ํจ์ฑ ๊ฒ์ฌ
function emailValid() {
const emailValue = $("#email1").val() + '@' + $("#email2").val();
if (!emailRegex.test(emailValue)) {
alert("์ ํจํ์ง ์์ ์ด๋ฉ์ผ์
๋๋ค");
$("#email1").focus();
return false;
} else {
return true;
}
}
//ํด๋ํฐ : ์ซ์๋ง 4์๋ฆฌ -> 3๋ฒ์งธ๋ก
$("#ph2").on("input", function () {
moveFocus(this, 4, "#ph3")
});
//์ฃผ๋ฏผ๋ฒํธ : ์ซ์๋ง ์ฃผ๋ฏผ๋ฒํธ ์ ํจ์ฑ
// ๋ท์๋ฆฌ ๋งจ์ฒซ์๋ฆฌ๋ง ์ซ์ 1******
// (๊ฐ์
์ ์ฒญํด๋ฆญ์)
// ์ฃผ๋ฏผ๋ฒํธ ์์๋ฆฌ 6๊ธ์
$("#jumin1").keyup(function () {
if ($("#jumin1").val().length > 7) {
$("#jumin1").val($("#jumin1").val().slice(0, 5));
}
;
});
//์ฃผ๋ฏผ๋ฒํธ ๋ท์๋ฆฌ ๋ฐ์์ค๊ธฐ
$("#jumin2").keyup(function () {
if ($("#jumin2").val().length > 8) {
$("#jumin2").val($("#jumin2").val().slice(0, 6));
}
;
//์๋กญ๊ฒ ์
๋ ฅ
let jumin2 = $("#jumin2").val().charAt($("#jumin2").val().length - 1);
//jumin3 ์ ์ถ๊ฐ
if ($("#jumin2").val().length > $("#jumin3").val().length) {
$("#jumin3").val($("#jumin3").val() + jumin2);
} else if ($("#jumin2").val().length < $("#jumin3").val().length) {
$("#jumin3").val($("#jumin3").val().slice(0, $("#jumin2").val().length - 1));
}
if ($("#jumin2").val().length > 1) {
$("#jumin2").val($("#jumin2").val().slice(0, -1) + '*');
}
});
//๋ท์๋ฆฌ *๋ก ๋ณ๊ฒฝํ๊ธฐ
//์ฃผ๋ฏผ๋ฒํธ ์ ํจ์ฑ ๊ฒ์ฌ
$("#testBtn").click(function () {
console.log("jumin2: " + $("#jumin2").val());
console.log("jumin3: " + $("#jumin3").val());
});
});
'๐ฅ๏ธ ํ๋ก ํธ์๋ > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ณต์ ] ํฐ์คํ ๋ฆฌ ์๋ ๋ชฉ์ฐจ ๋ง๋ค๊ธฐ, ๋ฐ๋ผ๋ค๋๋ ์๋ ๋ชฉ์ฐจ ์์ฑ ๋ฐฉ๋ฒ, ๋ชฉ์ฐจ ๋ค๋น๊ฒ์ดํฐ ๋ง๋ค๊ธฐ (์์ ์ฌ์ ์ด๋ณด์ ๊ฐ๋ฅ) (1) | 2023.12.09 |
---|---|
[Script] ์ฒดํฌ๋ฐ์ค (1) | 2023.12.07 |
[JQuery] ์ ์ด์ฟผ๋ฆฌ ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ (1) | 2023.12.05 |
[Node.js] Node.js ๋ง๋ณด๊ธฐ (0) | 2023.11.20 |
[JS] defer ์์ฑ (0) | 2023.11.12 |