PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

call javascript function upon validation error

  1. edmire
    Member

    Is there anyway to call a javascript function upon validation error?

    Let's say I have a simple text field that is required. If I click Submit without populating the field, I get "There was a problem with your submission.
    Errors have been highlighted below". It's great but I'd like to be able to call a javascript function in addition to displaying the error message. What would be the best way to accomplish that? Thanks.

    Posted 13 years ago on Friday January 21, 2011 | Permalink
  2. Currently there is no javascript event that you can bind to for a validation error; however, what you can do is check for the existence of the validation error div which has the class ".validation_error". If found, you'll know that you'll want to run your code.

    <script type="text/javascript">
    jQuery(document).ready(function(){
    
        if(jQuery("div.validation_error").length != 0){
    
            // code that should be run if the validation error exists goes here
    
        }
    
    });
    </script>

    Double check that code as I just wrote it in this forum post on the fly. Should do the trick though.

    Posted 13 years ago on Friday January 21, 2011 | Permalink
  3. edmire
    Member

    Thanks!

    Posted 13 years ago on Friday January 21, 2011 | Permalink
  4. Hi,

    I have a similar problem. Need to Call a script after the Ajax loads the validation error. How I do that?

    The solution above only works with ajax off

    Posted 12 years ago on Wednesday September 5, 2012 | Permalink
  5. Do you have a link to your form please? Thanks

    Posted 12 years ago on Wednesday September 5, 2012 | Permalink
  6. You can see here: http://maco.la/disbrave at footer. NEWSLETTER form

    I have a script that change the "value" for placeholder but after subimit with ajax, the form need to load the script again.

    I`ts the functions => info_scripts();

    Posted 12 years ago on Thursday September 6, 2012 | Permalink
  7. I believe the following will do the trick for you. Place it in your theme's function.php file.

    add_action('gform_register_init_scripts', 'register_custom_init_script', 10, 2);
    function register_custom_init_script($form, $field_values){
        $script =  "info_scripts();";
        GFFormDisplay::add_init_script($form['id'], 'my_custom_script', GFFormDisplay::ON_PAGE_RENDER, $script);
    }
    Posted 12 years ago on Monday September 10, 2012 | Permalink

This topic has been resolved and has been closed to new replies.