umbraco/Umbraco.Forms.Issues

Call a javascript function on form submission.

Closed this issue · 2 comments

Can someone please share their insights on how can we call a javascript function during form submission in Umbraco frontend.

Since, I am implementing an invisible recaptcha we have to trigger grecaptcha.execute(); to generate a client token.

I have to execute this script on form submission so that the recaptcha token gets generated.

<script type="text/javascript">
        function  onSubmit(event){
            event.preventDefault();
            grecaptcha.execute();
        }
    </script>

And then in the ValidateField method using

 var reCaptchaResponse = context.Request.Form["g-recaptcha-response"].ToString();

I can get the client token for validation.

Any leads are appreciated !

You can see how we've implemented this in the existing reCAPTCHA field by looking at the file staticwebassets/Assets/recaptcha.v3.init.js which you can find within the Umbraco.Forms.StaticAssets NuGet package (viewable online here).

Hope that helps, but I'm going to close this as the issue tracker isn't really intended as a place for general questions, I'd suggest going to the Umbraco forum for the product where you'll likely get a wider audience for questions you have.

Thanks @AndyButland for sharing the great insights. I have followed similar approach of using a timer function and works quite well for my use case.