Skip to content

Instantly share code, notes, and snippets.

@cytrowski
Forked from rmrotek/email.js
Last active January 11, 2019 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cytrowski/b8b140aaee0cb9b6a8d6648e1994fd31 to your computer and use it in GitHub Desktop.
Save cytrowski/b8b140aaee0cb9b6a8d6648e1994fd31 to your computer and use it in GitHub Desktop.
random code
var emailForm = document.getElementById("emailForm");
function report(item, status) {
console.log("item " + (status ? "succeed" : "failed"));
}
function handleForm(event) {
event.preventDefault();
const checkboxIsValid = document.getElementById("myCheck").checked;
const email = document.getElementById("getEmail").value;
const emailIsValid = email.indexOf("@") > 0 && email.indexOf(".") > 0;
const formIsValid = checkboxIsValid && emailIsValid;
document.getElementById("remind").style.display = checkboxIsValid
? "none"
: "block";
report("checkbox", checkboxIsValid);
report("email", emailIsValid);
if (formIsValid) {
// return location.replace("https://www.wp.pl/")
return console.log("redirecting...");
}
}
emailForm.addEventListener("submit", handleForm);
var emailForm = document.getElementById("emailForm");
function report(item, status) {
console.log("item " + (status ? "succeed" : "failed"));
}
const remind = document.getElementById("remind")
const fields = [
{
name: 'checkbox',
getValue: () => document.getElementById("myCheck").checked,
validate: value => value,
success: () => {
remind.style.display = 'none'
},
fail: () => {
remind.style.display = 'block'
}
},
{
name: 'email',
getValue: () => document.getElementById("getEmail").value,
validate: value => value.indexOf('@') > 0 && value.indexOf('.') > 0
}
]
function handleForm(event) {
event.preventDefault();
const result = fields.map(
field => ({
...field,
isValid: validate(field.getValue()),
})
)
result.map(field => field.isValid ? field.success : field.fail).filter(Boolean).forEach(f)
result.map(field => report(field.name, field.isValid))
if (result.every(field => field.isValid)) {
// return location.replace("https://www.wp.pl/")
return console.log("redirecting...");
}
}
emailForm.addEventListener("submit", handleForm);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment