I've been working on this form and have come to a roadblock. I'm trying to stop the form from submitting if it fails validation. The form itself is using formspree.io to collect the results and email them. I set out to write this in all js.
I think that because of this adding return false to the end of any validation does not work because formspree is doing something extra that I am not aware of/ don't understand.
Here is my code without the form elements but just validation logic and submit button. You can look at all the code for the form here.Any help or hints or direction would be appreciated thanks.
var app = document.createElement('form');
app.name = 'formIT'
app.action = "https://formspree.io/alaw989@gmail.com"
app.method = "POST"
//all div elements and stuff used to build the form would be here//
var submit = document.createElement('button');
submit.textContent = 'Submit'
app.appendChild(submit)
submit.style.cssText = "display: block; margin: 40px 0px 0px 20px; background-color: #1DA2F5; border: none; color: white; padding: 16px 32px; text-decoration: none; cursor: pointer;"
submit.type = "submit"
submit.value = "Send"
submit.addEventListener('click', function() {
var x = emailInput.value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
alert("Not a valid e-mail address");
}
var yes1 = radio1a.checked;
var no1 = radio1b.checked;
if (yes1 == "" && no1 == "") {
alert("Please answer question 1")
}
var yes2 = radio2a.checked;
var no2 = radio2b.checked;
if (yes2 == "" && no2 == "") {
alert("Please answer question 2")
}
var yes3 = radio3a.checked;
var no3 = radio3b.checked;
if (yes3 == "" && no3 == "") {
alert("Please answer question 3")
}
var yes4 = radio4a.checked;
var no4 = radio4b.checked;
if (yes4 == "" && no4 == "") {
alert("Please answer question 4")
}
var yes5 = radio5a.checked;
var no5 = radio5b.checked;
if (yes5 == "" && no5 == "") {
alert("Please answer question 5")
}
var yes6 = radio6a.checked;
var no6 = radio6b.checked;
if (yes6 == "" && no6 == "") {
alert("Please answer question 6")
}
var a = input1.value
var b = input2.value
if (a === "") {
alert("Missing Question 7")
}
if (b === "") {
alert("Missing Question 8")
}
});