Hi all - Is it possible to use a custom hook snippet in a formit form to perform a jquery validation of a form element? I am using hCaptcha on a form and want to check the element has been completed before the form can be submited. The jQuery is as below:
$(“form”).submit(function(event) {
var hcaptchaVal = $(‘[name=h-captcha-response]’).value;
if (hcaptchaVal === “”) {
event.preventDefault();
alert(“Please complete the hCaptcha”);
}
});
I was hoping to run this from the custom hook snippet - but it doesnt want to work - I just get a blank white page on form submit - assuming the snippet falls over trying to execute the jQuery… Any thoughts on a solution here?
A hook snippet is to run PHP code on the server.
If you want to run Javascript code on the client, just include it with a <script> tag (in your chunk/template/resource-content) like any other js code.
Thanks - This seems to work nicely from a validation point of view - but if the hCaptcha is not completed - the error message is not being rendered. Can you advise how best to get the error message to appear?
This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.