Skip to content

Instantly share code, notes, and snippets.

@benjsicam
Last active March 3, 2022 00:34
Show Gist options
  • Save benjsicam/5545756 to your computer and use it in GitHub Desktop.
Save benjsicam/5545756 to your computer and use it in GitHub Desktop.
A client side script which executes on submit of an Online Lead Form and sends a POST request to a back-end suitelet which does the reCaptcha verification.
/*
* This is the external URL of the Suitelet we've created on Part 3 of this tutorial.
*/
var CAPTCHA_VERIFICATION_SUITELET_URL = 'https://forms.na1.netsuite.com/app/site/hosting/scriptlet.nl?script=89&deploy=1&compid=TSTDRV262509&h=cd6d5783f52fba53b0f3';
/**
* Executes when the user clicks the Submit button.
* @returns {Boolean} True to continue save, false to abort save
*/
function onSubmit() {
var captchaChallenge = $('#recaptcha_challenge_field').val(); //Get the value of the recaptcha_challenge_field
var captchaResponse = $('#recaptcha_response_field').val(); //Get the value of the recaptcha_response_field
var isToBeSubmitted = true; //Denotes if the form will be submitted or not.
$.ajax({
url: CAPTCHA_VERIFICATION_SUITELET_URL + '&challenge=' + captchaChallenge + '&response=' + captchaResponse, //include the challenge and the response parameters
type: 'POST', //Do a POST request
accepts: 'application/json',
dataType: 'json',
cache: false,
/*
* This is important since jQuery.ajax is asynchronous by default.
* We need to be able to process the response first before we can tell that we can submit the form or not.
* So we set async to false.
*/
async: false
}).done(function (data) {
if (!data.status.isSuccess) { //If the status returned by the suitelet is not successful, then fail the verification.
alert('Captcha Verification Failed.');
Recaptcha.reload();
isToBeSubmitted = false; //Set this variable to false so that the form will not get submitted to the server.
}
});
return isToBeSubmitted; //If this is true, then the form submits else it prevents the user to submit the form.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment