How can I execute a call to a javascript function on submit, only if an embedded form validates?

  • thishandle
    Asked on May 4, 2019 at 7:39 PM

    I need to make a call to a javascript function on submission of my embedded jotform, only if the embedded jotform passes validation. I'm attempting to push data to another API only if the form is submitted successfully.

    Executing this call from a redirect URL after submission, like a thank you page, is not ideal. I also can not change the embedded markup of the jotform in anyway, as the end user will not be savvy enough to reliable update customized markup.

    Is there a method I can check that would return true or false based on a validation check of form elements? (eg: var is_valid = jotform.validate();)

    Thoughts? Thanks in advance!

  • jherwin
    Replied on May 4, 2019 at 11:26 PM

    This will not be possible to do in the Form Builder since it will not allow you to enter the JavaScript code, to achieve this you will need to get the source code of your form and then apply the needed JavaScript code to call your function when it passed the validation. 

    This guide will help you to get the source code of your form: How-to-get-the-Full-Source-Code-of-your-Form

    You can also check out our API guides/documents here if you would like to know more about the commands: https://api.jotform.com/docs/

  • thishandle
    Replied on May 5, 2019 at 1:40 AM

    I have embedded the fully generated source code of my jotform into my webpage as described in the link provided.

    Using the source code of the form on my webpage, is there a javascript function I can call to check form validation?

    What javascript methods are available to me via jotform to use with my form?

  • Welvin Support Team Lead
    Replied on May 5, 2019 at 8:22 AM

    I'm sorry, but the scripts in our form source codes cannot be altered and are not recommended to be altered. You need to have your validation in the form - be it a required validation, numeric, email, phone, address, etc. That is the purpose of the source codes option - to be able to add your script in the form and process offsite form requests. I presumed you are well versed with Javascript. 

  • thishandle
    Replied on May 5, 2019 at 1:29 PM

    This worked for my purposes:

    JotForm.validateAll('.jotform-form');
    //returns true if all form fields are valid and false if a field fails validation

    The JotForm object is created with the scripts in the embed source code. Using Chrome's dev tools console I was able find the method above. There are a lot of other methods there that would be useful to developers if documented some where. 

  • Mike
    Replied on May 5, 2019 at 5:25 PM

    Thank you for your input. We are glad to hear that you have managed to solve this.

    At this time we do not offer documentation for source code.