Title: Dynamically adding additional fields
Last modified: August 30, 2016

---

# Dynamically adding additional fields

 *  [jonatshift](https://wordpress.org/support/users/jonatshift/)
 * (@jonatshift)
 * [10 years, 12 months ago](https://wordpress.org/support/topic/dynamically-adding-additional-fields/)
 * Hi,
 * I would like to dynamically add a bunch of fields to a form. I managed to get
   this working with a workaround but then the fields don’t validate. How can I 
   validate some fields dynamically added to a form?
 * This is the code I used to dynamically add fields to the form:
 *     ```
       wpcf7_add_shortcode('contactform_newcontact', 'contactform_add_newcontact', true);
       function contactform_add_newcontact() {
   
       	$formhtml = preg_replace( "/\r|\n/", "", '<input type="text" name="textbox1[]" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false">
       	<br />
       	<input type="text" name="textbox2[]" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false">
       	<br />
       	<input type="text" name="textbox3[]" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false">
       	<br />
       	<input type="text" name="textbox4[]" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false">');
   
       	$output = $formhtml . '<div name="cf-btn-wrapper" id="cf-btn-wrapper"><input type="button" name="cf-addbtn[]" id="cf-addbtn[]" value="Add Another" class="wpcf7-form-control wpcf7-submit avia-button avia-size-large cf-addcontact"></div>';
   
       	$output .= '<script type="text/javascript">
          jQuery(document).ready(function() {
             jQuery(".cf-addcontact").live("click",function() {
                jQuery("#cf-btn-wrapper").before(\'' . $formhtml . '\');
                jQuery("form.wpcf7-form").validate(); // This doesn't work
             });
          });
       </script>';
   
       	return $output;
       }
       ```
   
 * I found this article but it’s for a specific field that pre-exists on the form:
   [http://contactform7.com/2015/03/28/custom-validation/](http://contactform7.com/2015/03/28/custom-validation/)
 * How can I invalidate a dynamically added textbox? i.e. calling something like
   jQuery(“form.wpcf7-form”).validate(); after the fields have been added to re-
   validate all fields? Or hook into validation server side?
 * [https://wordpress.org/plugins/contact-form-7/](https://wordpress.org/plugins/contact-form-7/)

The topic ‘Dynamically adding additional fields’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [jonatshift](https://wordpress.org/support/users/jonatshift/)
 * Last activity: [10 years, 12 months ago](https://wordpress.org/support/topic/dynamically-adding-additional-fields/)
 * Status: not resolved