generated from tod/odin-webpack-template
Compare commits
No commits in common. "12eb673c902561bba82c6c752a5e8684db30d326" and "27117cd6414733a09d3b5e2dfcfba5092a7f7523" have entirely different histories.
12eb673c90
...
27117cd641
|
@ -12,9 +12,10 @@
|
||||||
<input name="email" id="email" type="email" required placeholder="Email (christopher@example.com)">
|
<input name="email" id="email" type="email" required placeholder="Email (christopher@example.com)">
|
||||||
|
|
||||||
<input name="country" id="country" type="text" list="countries-list" required placeholder="Country (United Kingdom)">
|
<input name="country" id="country" type="text" list="countries-list" required placeholder="Country (United Kingdom)">
|
||||||
|
<output id="country-info"></output>
|
||||||
<datalist id="countries-list"></datalist>
|
<datalist id="countries-list"></datalist>
|
||||||
|
|
||||||
<input name="zip" id="zip" type="text" required placeholder="ZIP code (12-345)">
|
<input name="zip" id="zip" type="text" required placeholder="ZIP code (12-345)" pattern="\d{2}-\d{3}">
|
||||||
|
|
||||||
<input name="password" id="password" type="password" required placeholder="Password (Pa$Sw0rD)">
|
<input name="password" id="password" type="password" required placeholder="Password (Pa$Sw0rD)">
|
||||||
<div id="password-check">
|
<div id="password-check">
|
||||||
|
|
31
src/main.js
31
src/main.js
|
@ -41,34 +41,23 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||||
const formData = new FormData(form);
|
const formData = new FormData(form);
|
||||||
const formDataValue = formData.values();
|
const formDataValue = formData.values();
|
||||||
const [email, country, zip] = Array(5).fill(formDataValue.next().value);
|
const [email, country, zip] = Array(5).fill(formDataValue.next().value);
|
||||||
setTimeout(() => {
|
alert(
|
||||||
alert(
|
`Success!\nEmail: ${email}\nCountry: ${country}\nZIP: ${zip}\nPassword: [redacted]`,
|
||||||
`Success!\nEmail: ${email}\nCountry: ${country}\nZIP: ${zip}\nPassword: [redacted]`,
|
);
|
||||||
);
|
|
||||||
}, 600);
|
|
||||||
} else {
|
} else {
|
||||||
alert("Hold up, one or more fields are invalid! Try again");
|
alert("Hold up, one or more fields are invalid! Try again");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
countryInput.addEventListener("input", () => {
|
countryInput.addEventListener("input", () => {
|
||||||
const valid = countryInput.value.search(COUNTRY_REGEX) >= 0;
|
countryInput.setAttribute(
|
||||||
countryInput.setAttribute("input-valid", valid);
|
"input-valid",
|
||||||
if (valid) {
|
countryInput.value.search(COUNTRY_REGEX) >= 0,
|
||||||
countryInput.setCustomValidity("");
|
);
|
||||||
} else {
|
|
||||||
countryInput.setCustomValidity("Invalid country");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
zipInput.addEventListener("input", () => {
|
countryInput.addEventListener("change", () => {
|
||||||
const valid = zipInput.value.search(/\d{2}-\d{3}/) >= 0;
|
document.querySelector("#country-info").textContent =
|
||||||
zipInput.setAttribute("input-valid", valid);
|
countryInput.value.search(COUNTRY_REGEX) >= 0 ? "" : "Invalid country";
|
||||||
if (valid) {
|
|
||||||
zipInput.setCustomValidity("");
|
|
||||||
} else {
|
|
||||||
zipInput.setCustomValidity("ZIP Code format should be 12-345");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
passwordInput.addEventListener("input", checkPasswordValidity);
|
passwordInput.addEventListener("input", checkPasswordValidity);
|
||||||
|
|
|
@ -19,20 +19,18 @@ function checkPasswordMatch() {
|
||||||
passwordInput.value === ""
|
passwordInput.value === ""
|
||||||
) {
|
) {
|
||||||
passwordMatch.style.backgroundColor = "";
|
passwordMatch.style.backgroundColor = "";
|
||||||
passwordConfirmInput.setCustomValidity("Passwords don't match");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
passwordMatch.style.backgroundColor = "#4b4";
|
passwordMatch.style.backgroundColor = "#4b4";
|
||||||
passwordConfirmInput.setCustomValidity("");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkPasswordRequirement(regex, requirementElement) {
|
function checkPasswordRequirement(e, regex, element) {
|
||||||
if (passwordInput.value.search(regex) >= 0) {
|
if (e.value.search(regex) >= 0) {
|
||||||
requirementElement.style.backgroundColor = "#4b4";
|
element.style.backgroundColor = "#4b4";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
requirementElement.style.backgroundColor = "";
|
element.style.backgroundColor = "";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,18 +41,23 @@ function checkPasswordValidity() {
|
||||||
} else {
|
} else {
|
||||||
passwordLength.style.backgroundColor = "";
|
passwordLength.style.backgroundColor = "";
|
||||||
}
|
}
|
||||||
const lowercase = checkPasswordRequirement(/[a-z]/, passwordLowercase);
|
const lowercase = checkPasswordRequirement(
|
||||||
const uppercase = checkPasswordRequirement(/[A-Z]/, passwordUppercase);
|
passwordInput,
|
||||||
const number = checkPasswordRequirement(/\d/, passwordNumber);
|
/[a-z]/,
|
||||||
const special = checkPasswordRequirement(/[#?!@$%^&*-]/, passwordSpecial);
|
passwordLowercase,
|
||||||
const suitsRequirements =
|
);
|
||||||
length && lowercase && uppercase && number && special;
|
const uppercase = checkPasswordRequirement(
|
||||||
if (suitsRequirements) {
|
passwordInput,
|
||||||
passwordInput.setCustomValidity("");
|
/[A-Z]/,
|
||||||
} else {
|
passwordUppercase,
|
||||||
passwordInput.setCustomValidity("Password does not meet requirements");
|
);
|
||||||
}
|
const number = checkPasswordRequirement(passwordInput, /\d/, passwordNumber);
|
||||||
return suitsRequirements;
|
const special = checkPasswordRequirement(
|
||||||
|
passwordInput,
|
||||||
|
/[#?!@$%^&*-]/,
|
||||||
|
passwordSpecial,
|
||||||
|
);
|
||||||
|
return length && lowercase && uppercase && number && special;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { checkPasswordValidity, checkPasswordMatch };
|
export { checkPasswordValidity, checkPasswordMatch };
|
||||||
|
|
Loading…
Reference in New Issue