Skip to content

Instantly share code, notes, and snippets.

@scmmishra
Last active January 25, 2024 02:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scmmishra/f22d0ea9ebc53a752daa0e9a39f35e33 to your computer and use it in GitHub Desktop.
Save scmmishra/f22d0ea9ebc53a752daa0e9a39f35e33 to your computer and use it in GitHub Desktop.
Using reCaptcha to Validate Web Forms in ERPNext
// Create a reCaptcha v2 on https://www.google.com/recaptcha/admin/
// Copy the site key and paste it below
// Add a HTML field in the Lead DocType with fieldname "captcha_html_wrapper"
// Add the HTML field at the end of the fields child table
// Add the following to web form script
frappe.require("https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit&type=api.js")
let el = document.querySelector('[data-fieldname="captcha_html_wrapper"]');
window.captcha_failed = true;
frappe.web_form.validate = () => {
if (window.captcha_failed) {
frappe.throw("You need to finish the captcha challenge");
}
}
window.onloadCallback = function() {
grecaptcha.render(el, {
'sitekey' : '<site-key>', // Paste Site Key Here
'callback': () => {
window.captcha_failed = false;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment