Notification Bar
Enjay Gurukul
Learn, Certify & Excel, With Our Micro-Learning Courses
Explore Now document.addEventListener('DOMContentLoaded', function () {
console.log("Enhanced email validation script loaded...");// Function to check if the URL contains a specific query parameter
function isCodeParameterPresent() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.has('code');
}// List of commonly known personal email domains
const blockedDomains = [
'gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'aol.com',
'rediffmail.com', 'mail.com', 'protonmail.com', 'zoho.com', 'icloud.com',
'yandex.com', 'live.com', 'msn.com', 'gmx.com', 'ymail.com'
];// Function to check for fuzzy matches
function isBlockedDomain(email) {
const domain = email.split('@')[1]?.toLowerCase() || '';
return blockedDomains.some(blocked => getSimilarity(blocked, domain) >= 0.8);
}// Function to calculate string similarity (for typos like "gmale.com")
function getSimilarity(str1, str2) {
let longer = str1, shorter = str2;
if (str1.length < str2.length) {
longer = str2; shorter = str1;
}
const longerLength = longer.length;
if (longerLength === 0) return 1.0;
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength);
}function editDistance(s1, s2) {
const costs = [];
for (let i = 0; i <= s1.length; i++) {
let lastValue = i;
for (let j = 0; j <= s2.length; j++) {
if (i === 0) costs[j] = j;
else if (j > 0) {
let newValue = costs[j - 1];
if (s1.charAt(i - 1) !== s2.charAt(j - 1)) newValue = Math.min(newValue, lastValue, costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
if (i > 0) costs[s2.length] = lastValue;
}
return costs[s2.length];
}// Function to validate email
function validateEmail(emailField, submitButton) {
const email = emailField.value.trim();
const isInvalid = isBlockedDomain(email);if (isInvalid) {
emailField.setCustomValidity('Please provide your work email.');
emailField.nextElementSibling.style.display = 'block';
submitButton.disabled = true;
} else {
emailField.setCustomValidity('');
emailField.nextElementSibling.style.display = 'none';
submitButton.disabled = false;
}
}// Check if the "code" parameter is present
const codeParameterPresent = isCodeParameterPresent();// Attach validation logic to Elementor forms only if "code" parameter is not present
if (!codeParameterPresent) {
document.querySelectorAll('.elementor-form').forEach(function (form) {
const emailField = form.querySelector('input[type="email"]');
const submitButton = form.querySelector('button[type="submit"]');if (emailField && submitButton) {
// Add a message below the email field
if (!emailField.nextElementSibling || emailField.nextElementSibling.tagName !== 'DIV') {
const warningMessage = document.createElement('div');
warningMessage.style.color = 'red';
warningMessage.style.fontSize = '14px';
warningMessage.style.marginTop = '5px';
warningMessage.textContent = 'Please provide your work email.';
warningMessage.style.display = 'none';
emailField.insertAdjacentElement('afterend', warningMessage);
}// Validate on input change
emailField.addEventListener('input', function () {
validateEmail(emailField, submitButton);
});// Validate again before submission
submitButton.addEventListener('click', function (e) {
validateEmail(emailField, submitButton);
if (submitButton.disabled) {
e.preventDefault();
}
});
}
});
} else {
console.log("Validation disabled due to 'code' parameter in URL.");
}
});Skip to content