I wrote this JavaScript code to tell me if two email addresses do not match. That part works just fine.
The problem I am having is that, even if the email adresses do not match, it will still let me submit the forms.
I am just wondering how would I make it where you can not submit the form if the email addresses do not match.
Here is my code:
"use strict";
// global variables
var profile = {};
// validate entered password
function validateEmail() {
var email1Input = document.getElementById("email");
var email2Input = document.getElementById("email_retype");
email1Input.value = email1Input.value.toLowerCase();
email2Input.value = email2Input.value.toLowerCase();
var errorDiv = document.getElementById("emailError");
try {
if (email1Input.value.localeCompare(email2Input.value) !== 0) {
throw "The e-mails do not match";
}
// remove any password error styling and message
email1Input.style.background = "";
email2Input.style.background = "";
errorDiv.style.display = "none";
errorDiv.innerHTML = "";
}
catch(msg) {
// display error message
errorDiv.style.display = "block";
errorDiv.innerHTML = msg;
// change input style
email1Input.style.background = "rgb(255,233,233)";
email2Input.style.background = "rgb(255,233,233)";
}
}
function createEventListeners() {
var email2Input = document.getElementById("email_retype");
if (email2Input.addEventListener) {
email2Input.addEventListener("change", validateEmail, false);
} else if (email2Input.attachEvent) {
email2Input.attachEvent("onchange", validateEmail);
}
var lodgings = document.getElementsByName("lodgings");
if (lodgings[0].addEventListener) {
for (var i = 0; i < lodgings.length; i++) {
lodgings[i].addEventListener("change", registerLodging, false);
}
} else if (lodgings[0].attachEvent) {
for (var i = 0; i < lodgings.length; i++) {
lodgings[i].attachEvent("onchange", registerLodging);
}
}
}
if (window.addEventListener) {
window.addEventListener("load", createEventListeners, false);
} else if (window.attachEvent) {
window.attachEvent("onload", createEventListeners);
}