Title: Validate Input Fields
Last modified: August 31, 2016

---

# Validate Input Fields

 *  Resolved [djuniverss](https://wordpress.org/support/users/djuniverss/)
 * (@djuniverss)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/validate-input-fields/)
 * Great plugin by the way!
 * I would like to validate an input field created with this plugin upon adding 
   to cart.
 * For example I have a field “gender” and would like to check if entered value 
   is either “M” or “F”.
 * Can you please let me know how to do this?
 * Thanks
 * [https://wordpress.org/plugins/wc-fields-factory/](https://wordpress.org/plugins/wc-fields-factory/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [Saravana Kumar K](https://wordpress.org/support/users/mycholan/)
 * (@mycholan)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/validate-input-fields/#post-6974318)
 * Hi, you could do something like this
 *     ```
       add_filter( 'woocommerce_add_to_cart_validation', 'validate_field', 10, 2 );
       function validate_field( $passed, $pid = null ) {
       	$res = true;
       	if( isset( $pid ) ) {
       		$all_fields = apply_filters( 'wccpf/load/all_fields', $pid );
       		foreach ( $all_fields as $fields ) {
       			foreach ( $fields as $field ) {
       				if( $field["name"] == "your_field_name" && isset( $_REQUEST[ $field["name"] ] ) ) {
       					$val = $_REQUEST[ $field["name"] ];
       					if( $val != "M" || $val != "F" ) {
       						$res = false;
       					}
       				}
       			}
       			if( !$res ) {
       				wc_add_notice( 'Validation Failed', 'error' );
       			}
       		}
       	}
       	return $res;
       }
       ```
   
 *  Thread Starter [djuniverss](https://wordpress.org/support/users/djuniverss/)
 * (@djuniverss)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/validate-input-fields/#post-6974371)
 * Thank you for your quick reply.
    That works perfectly!!
 * It could be a useful feature to included in the future.
 * Cheers
 *  Thread Starter [djuniverss](https://wordpress.org/support/users/djuniverss/)
 * (@djuniverss)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/validate-input-fields/#post-6974377)
 * The validation code works but when a field fails validation, all input fields
   get cleared.
 * The user will have to re-enter all the input values again in order to add to 
   cart.
 * Is there a way around this?
    I have 10 input fields making it unpleasant for 
   the user experience to re-enter.
 *  Plugin Author [Saravana Kumar K](https://wordpress.org/support/users/mycholan/)
 * (@mycholan)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/validate-input-fields/#post-6974449)
 * Hi, this option is under progress, will be available with my next release.
 *  Thread Starter [djuniverss](https://wordpress.org/support/users/djuniverss/)
 * (@djuniverss)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/validate-input-fields/#post-6974533)
 * Thanks! much appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Validate Input Fields’ is closed to new replies.

 * ![](https://ps.w.org/wc-fields-factory/assets/icon-128x128.jpg?rev=2738843)
 * [WC Fields Factory](https://wordpress.org/plugins/wc-fields-factory/)
 * [Support Threads](https://wordpress.org/support/plugin/wc-fields-factory/)
 * [Active Topics](https://wordpress.org/support/plugin/wc-fields-factory/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wc-fields-factory/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wc-fields-factory/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [djuniverss](https://wordpress.org/support/users/djuniverss/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/validate-input-fields/#post-6974533)
 * Status: resolved