-
-
Save cytrowski/b8b140aaee0cb9b6a8d6648e1994fd31 to your computer and use it in GitHub Desktop.
random code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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