custom-cleave
Advanced tools
Comparing version 1.0.10 to 1.0.11
{ | ||
"name": "custom-cleave", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "Format for input element", | ||
@@ -5,0 +5,0 @@ "main": "src/custom-cleave.js", |
@@ -6,116 +6,170 @@ const dateFormatter = (options) => { | ||
// let d = 0, | ||
// m = 0, | ||
// y = 0; | ||
const libCharacter = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]; | ||
let d = 0, | ||
m = 0, | ||
y = 0; | ||
const targetInput = document.getElementById(`${id}`); | ||
if (targetInput) { | ||
targetInput.maxLength = 10; | ||
targetInput.setAttribute("type", "tel"); | ||
function addPlaceHolder(type) { | ||
if (type == "y") return "YYYY"; | ||
if (type == "d") return "DD"; | ||
if (type == "m") return "MM"; | ||
} | ||
let plc = ""; | ||
pattern.forEach((type, index) => { | ||
if (index < 2) plc = plc + addPlaceHolder(type) + delimiter; | ||
else plc = plc + addPlaceHolder(type); | ||
}); | ||
function addPlaceHolder(type) { | ||
if (type == "y") return "YYYY"; | ||
if (type == "d") return "DD"; | ||
if (type == "m") return "MM"; | ||
} | ||
let plc = ""; | ||
pattern.forEach((type) => { | ||
plc = plc + addPlaceHolder(type) + delimiter; | ||
}); | ||
targetInput.placeholder = plc; | ||
targetInput.placeholder = plc; | ||
targetInput.maxLength = 10; | ||
targetInput.setAttribute("type", "tel"); | ||
function isLeapYear(year) { | ||
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; | ||
function isLeapYear(year) { | ||
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; | ||
} | ||
function checkCharacter(character) { | ||
return libCharacter.some((c) => c == character); | ||
} | ||
function fixDayFormMonth() { | ||
if ( | ||
(m == 1 || m == 3 || m == 4 || m == 7 || m == 8 || m == 10 || m == 12) && | ||
d >= 31 | ||
) | ||
d = 31; | ||
if ((m == 4 || m == 6 || m == 9 || m == 11) && d >= 30) d = 30; | ||
if (m == 2) { | ||
if (isLeapYear(y) && d >= 29) d = 29; | ||
if (!isLeapYear(y) && d >= 28) d = 28; | ||
} | ||
} | ||
function checkCharacter(character) { | ||
return libCharacter.some((c) => c == character); | ||
function validateDate() { | ||
if (d >= 31) d = 31; | ||
if (m >= 12) m = 12; | ||
// if (parseInt(y) == 0) y = "1970"; | ||
if (y >= 2100) y = 2100; | ||
} | ||
function validate(type, oldChar, newChar) { | ||
if (type == "d") { | ||
if (3 < parseInt(newChar) && parseInt(newChar) < 10) { | ||
targetInput.value = oldChar + "0" + newChar; | ||
if (targetInput.value.length < 9) addDelimiter(); | ||
} else { | ||
targetInput.value = oldChar + "" + newChar; | ||
} | ||
} | ||
if (type == "m") { | ||
if (1 < parseInt(newChar) && parseInt(newChar) < 10) { | ||
targetInput.value = oldChar + "0" + newChar; | ||
if (targetInput.value.length < 9) addDelimiter(); | ||
} else targetInput.value = oldChar + "" + newChar; | ||
} | ||
} | ||
function validate(type, oldChar, newChar) { | ||
if (type == "d") { | ||
if (3 < parseInt(newChar) && parseInt(newChar) < 10) { | ||
targetInput.value = oldChar + "0" + newChar; | ||
if (targetInput.value.length < 9) addDelimiter(); | ||
} else targetInput.value = oldChar + "" + newChar; | ||
// yyyy-mm-dd || yyyy-dd-mm || mm-dd-yyyy || dd-mm-yyyy || dd-yyyy-mm || mm-yyyy-dd | ||
function addDelimiter() { | ||
targetInput.value = targetInput.value + delimiter; | ||
} | ||
function format(oldChar, newChar) { | ||
let l = targetInput.value.length; | ||
if (pattern[0] == "y") { | ||
if (l == 4) { | ||
validateDate(); | ||
addDelimiter(); | ||
} | ||
if (type == "m") { | ||
if (1 < parseInt(newChar) && parseInt(newChar) < 10) { | ||
targetInput.value = oldChar + "0" + newChar; | ||
if (targetInput.value.length < 9) addDelimiter(); | ||
} else targetInput.value = oldChar + "" + newChar; | ||
if (l == 6) { | ||
validate(pattern[1], oldChar, newChar); | ||
} | ||
if (l == 7) { | ||
validateDate(); | ||
addDelimiter(); | ||
} | ||
if (l == 9) { | ||
validate(pattern[2], oldChar, newChar); | ||
} | ||
} | ||
// yyyy-mm-dd || yyyy-dd-mm || mm-dd-yyyy || dd-mm-yyyy || dd-yyyy-mm || mm-yyyy-dd | ||
function addDelimiter() { | ||
targetInput.value = targetInput.value + delimiter; | ||
if (pattern[1] == "y") { | ||
if (l == 2) { | ||
validateDate(); | ||
addDelimiter(); | ||
} | ||
if (l == 1) { | ||
validate(pattern[0], oldChar, newChar); | ||
} | ||
if (l == 7) { | ||
validateDate(); | ||
addDelimiter(); | ||
} | ||
if (l == 9) { | ||
validate(pattern[2], oldChar, newChar); | ||
} | ||
} | ||
function format(oldChar, newChar) { | ||
let l = targetInput.value.length; | ||
if (pattern[0] == "y") { | ||
if (l == 4) { | ||
addDelimiter(); | ||
} | ||
if (l == 6) { | ||
validate(pattern[1], oldChar, newChar); | ||
} | ||
if (l == 7) { | ||
addDelimiter(); | ||
} | ||
if (l == 9) { | ||
validate(pattern[2], oldChar, newChar); | ||
} | ||
if (pattern[2] == "y") { | ||
if (l == 1) { | ||
validate(pattern[0], oldChar, newChar); | ||
} | ||
if (pattern[1] == "y") { | ||
if (l == 2) { | ||
addDelimiter(); | ||
} | ||
if (l == 1) { | ||
validate(pattern[0], oldChar, newChar); | ||
} | ||
if (l == 7) { | ||
addDelimiter(); | ||
} | ||
if (l == 9) { | ||
validate(pattern[2], oldChar, newChar); | ||
} | ||
if (l == 2) { | ||
validateDate(); | ||
addDelimiter(); | ||
} | ||
if (pattern[2] == "y") { | ||
if (l == 1) { | ||
validate(pattern[0], oldChar, newChar); | ||
} | ||
if (l == 2) { | ||
addDelimiter(); | ||
} | ||
if (l == 4) { | ||
validate(pattern[1], oldChar, newChar); | ||
} | ||
if (l == 5) { | ||
addDelimiter(); | ||
} | ||
if (l == 4) { | ||
validate(pattern[1], oldChar, newChar); | ||
} | ||
if (l == 5) { | ||
validateDate(); | ||
addDelimiter(); | ||
} | ||
} | ||
} | ||
targetInput.addEventListener("input", (e) => { | ||
targetInput.addEventListener("keydown", (event) => { | ||
if (event.key == "Backspace") { | ||
targetInput.value = ""; | ||
} | ||
}); | ||
let newChar = e.target.value.substr(e.target.value.length - 1, 1), | ||
oldChar = e.target.value.slice(0, e.target.value.length - 1); | ||
if (!checkCharacter(newChar)) { | ||
targetInput.value = targetInput.value.slice( | ||
0, | ||
e.target.value.length - 1 | ||
); | ||
targetInput.addEventListener("input", (e) => { | ||
targetInput.addEventListener("keydown", (event) => { | ||
if (event.key == "Backspace") { | ||
targetInput.value = ""; | ||
} | ||
format(oldChar, newChar); | ||
}); | ||
} | ||
let newChar = e.target.value.substr(e.target.value.length - 1, 1), | ||
oldChar = e.target.value.slice(0, e.target.value.length - 1); | ||
if (pattern == ["m", "y", "d"]) { | ||
m = parseInt(targetInput.value.slice(0, 2)); | ||
y = parseInt(targetInput.value.slice(3, 7)); | ||
d = parseInt(targetInput.value.slice(8, 10)); | ||
} | ||
if (pattern == ["d", "y", "m"]) { | ||
d = parseInt(targetInput.value.slice(0, 2)); | ||
y = parseInt(targetInput.value.slice(3, 7)); | ||
m = parseInt(targetInput.value.slice(8, 10)); | ||
} | ||
if (pattern == ["y", "d", "m"]) { | ||
y = parseInt(targetInput.value.slice(0, 4)); | ||
d = parseInt(targetInput.value.slice(5, 7)); | ||
m = parseInt(targetInput.value.slice(8, 10)); | ||
} | ||
if (pattern == ["y", "m", "d"]) { | ||
y = parseInt(targetInput.value.slice(0, 4)); | ||
m = parseInt(targetInput.value.slice(5, 7)); | ||
d = parseInt(targetInput.value.slice(8, 10)); | ||
} | ||
if (pattern == ["d", "m", "y"]) { | ||
d = parseInt(targetInput.value.slice(0, 2)); | ||
m = parseInt(targetInput.value.slice(3, 5)); | ||
y = parseInt(targetInput.value.slice(6, 10)); | ||
} | ||
if (pattern == ["m", "d", "y"]) { | ||
m = parseInt(targetInput.value.slice(0, 2)); | ||
d = parseInt(targetInput.value.slice(3, 5)); | ||
y = parseInt(targetInput.value.slice(6, 10)); | ||
} | ||
if (!checkCharacter(newChar)) { | ||
targetInput.value = targetInput.value.slice(0, e.target.value.length - 1); | ||
} | ||
format(oldChar, newChar); | ||
console.log("d", d, "m", m, "y", y); | ||
}); | ||
}; | ||
@@ -122,0 +176,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6603
199