Skip to content

Instantly share code, notes, and snippets.

@heyJordanParker
Created January 7, 2025 09:44
Show Gist options
  • Select an option

  • Save heyJordanParker/91aa9ea645664d42c7f8e4427eb9f78a to your computer and use it in GitHub Desktop.

Select an option

Save heyJordanParker/91aa9ea645664d42c7f8e4427eb9f78a to your computer and use it in GitHub Desktop.
Custom validation bricksforge
const form = document.querySelector('.brxe-brf-pro-forms');
const submitButton = form.querySelector('button[type="submit"]');
let validationPassed = false;
// Intercept the submit button click
submitButton.addEventListener('click', async (e) => {
// If we've already validated, let it proceed
if (validationPassed) {
return;
}
e.preventDefault();
const formInstance = BrfProForms.createSilentInstance(form);
const emailField = form.querySelector('[name="form-field-email"]');
// Check if basic form validation passes first
if (!form.checkValidity()) {
return;
}
try {
formInstance.showLoading();
const isValid = await spamCheck(emailField.value);
if (!isValid) {
formInstance.markFieldAsInvalid(emailField, {
message: 'Invalid email according to my AI supercomputer. Try again.'
});
formInstance.hideLoading();
return;
}
// If valid, set flag and click button
validationPassed = true;
submitButton.click();
} catch (error) {
console.error("Error checking email:", error);
formInstance.markFieldAsInvalid(emailField, {
message: 'An error occurred while checking your email'
});
formInstance.hideLoading();
}
});
// Reset validation state when email changes
form.querySelector('[name="form-field-email"]').addEventListener('input', () => {
validationPassed = false;
});
// Success handler
form.addEventListener('brf_submit', (e) => {
if (e.detail.success) {
const email = form.querySelector('[name="form-field-email"]').value;
bentoIdentifyUser(email);
}
});
async function spamCheck(email) {
try {
const result = await bento.spamCheck(email);
return result;
} catch (error) {
console.error("Spam check error:", error);
throw error;
}
}
function bentoIdentifyUser(email) {
bento.identify(email);
bento.view();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment