, into an Embed Code field of the form (or the form's // theme), then fill in the four settings below. MIT licence. Guide: https://email-validation.org/docs/guides/formstack/ // // It uses only Formstack's Live Form API (live-form-api.formstack.com): window.fsApi().getForm(), a "change" listener // that checks the address as soon as the respondent leaves the Email field, and a "submit" listener that calls // event.preventDefault() when the result is one you stop. Valid and unknown addresses, a slow or failed check and a // key that is not a browser key never stop the form. (() => { const SETTINGS = { // The form's id: the number in the form builder's address. formId: 'YOUR_FORM_ID', // The Email field's id: the number in the form builder's address while the field is selected. emailFieldId: 'YOUR_EMAIL_FIELD_ID', // A browser key: Dashboard -> API keys -> Create key -> "On a public web page", with your form's addresses as // allowed origins and a daily credit cap. Anyone can read it in the page, so never paste any other key here. browserKey: 'YOUR_BROWSER_KEY', // 'https://api-eu.email-validation.org' to have the addresses validated in the EU. api: 'https://api.email-validation.org', // The results that stop the form: any of 'invalid', 'disposable', 'catch_all'. stop: ['invalid', 'disposable'], messages: { invalid: 'This address cannot receive email. Please check it.', disposable: 'Please use a permanent address, not a disposable one.', catch_all: 'We could not confirm this address. Please use another one.', }, }; /** How long a submit waits for a check still under way before the form goes anyway. */ const WAIT_MS = 3000; /** A request still open after this is dropped (the address then passes). */ const ABORT_MS = 10000; /** The API's shortest `timeout` (seconds): a check that needs longer ends as a free `unknown`. */ const SERVER_TIMEOUT_S = 5; const STOPPABLE = ['invalid', 'disposable', 'catch_all']; const key = String(SETTINGS.browserKey).trim(); if (key.slice(30, 37) !== 'browser') { console.warn('email-validation.org: not a browser key, so nothing is checked.'); return; } if (typeof window.fsApi !== 'function') { console.warn('email-validation.org: Formstack’s form API is not on this page, so nothing is checked.'); return; } const form = window.fsApi().getForm(String(SETTINGS.formId)); if (!form) { console.warn('email-validation.org: no form with that id on this page, so nothing is checked.'); return; } const fieldId = String(SETTINGS.emailFieldId); const stop = SETTINGS.stop.filter((s) => STOPPABLE.includes(s)); /** One check per address on this page: the address in lower case -> a promise of its status, or null. */ const checks = new Map(); /** The checks that have answered: the address in lower case -> its status, or null. */ const settled = new Map(); /** Set after a refused key or no credits (401, 402, 403): nothing more is sent from this page. */ let off = false; const addressOf = () => { const field = form.getField(fieldId); const value = field ? field.getValue() : null; const address = value && typeof value.value === 'string' ? value.value.trim() : ''; return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(address) && address.length <= 320 ? address : ''; }; const ask = async (address) => { if (off) return null; const ctrl = new AbortController(); const timer = setTimeout(() => ctrl.abort(), ABORT_MS); try { const res = await fetch(`${SETTINGS.api}/v1/validate`, { method: 'POST', headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ email: address, timeout: SERVER_TIMEOUT_S }), credentials: 'omit', // the page's origin at most, never its address (which could hold an answer in its query) referrerPolicy: 'strict-origin', signal: ctrl.signal, }); if (res.status > 400 && res.status < 404) off = true; if (!res.ok) return null; const body = await res.json(); return body && typeof body.status === 'string' ? body.status : null; } catch { return null; } finally { clearTimeout(timer); } }; const check = (address) => { const id = address.toLowerCase(); if (!checks.has(id)) { checks.set( id, ask(address).then((status) => { settled.set(id, status); return status; }), ); } return checks.get(id); }; /** The status, or null once `ms` have passed without one. */ const within = (promise, ms) => new Promise((resolve) => { const timer = setTimeout(() => resolve(null), ms); promise.then((status) => { clearTimeout(timer); resolve(status); }); }); const stopped = (status) => typeof status === 'string' && stop.includes(status); /** Stops the submit as Formstack's documented submit listener does: a message, then event.preventDefault(). */ const refuse = (event, status) => { window.alert(SETTINGS.messages[status]); event.preventDefault(); return undefined; }; form.registerFormEventListener({ type: 'change', onFormEvent: (event) => { if (event.data && String(event.data.fieldId) === fieldId) { const address = addressOf(); if (address) check(address); } return Promise.resolve(event); }, }); form.registerFormEventListener({ type: 'submit', onFormEvent: (event) => { const address = addressOf(); if (!address) return Promise.resolve(event); const id = address.toLowerCase(); // Checked when the respondent left the field (the usual case): decided at once. if (settled.has(id)) { const status = settled.get(id); return stopped(status) ? refuse(event, status) : Promise.resolve(event); } // Still being checked (or never was): wait at most WAIT_MS for it. return within(check(address), WAIT_MS).then((status) => (stopped(status) ? refuse(event, status) : event)); }, }); })();