Title: Sum validation
Last modified: December 11, 2016

---

# Sum validation

 *  Resolved [lillinoki](https://wordpress.org/support/users/lillinoki/)
 * (@lillinoki)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/sum-validation/)
 * Hi there!
 * I’m trying to insert an error message whether the sum of some input fields is
   greater than a certain number.
 * More specifically, the contact form is to enquire about the vacancy of hotel 
   room.
    For each room, I need to know how many people/pets are going to be in 
   that room. Of course, there is a limit to it, and that is 4 people/pets.
 * I’d like to insert an error message anytime they try to book a room for more 
   than 4 people/pets.
 * In the contact form, it would be something like:
 *     ```
       <label> Adults*
       [select* r1-adults "1" "2" "3" "4"] </label>
       <label> Children*
       [select* r1-children "0" "1" "2" "3"] </label>
       <label> Babies*
       [select* r1-babies "0" "1" "2" "3"] </label>
       <label> Pets*
       [select* r1-pets "0" "1" "2" "3"] </label>
       ```
   
 * In the functions.php:
 *     ```
       add_filter( 'wpcf7_max_capacity*', 'custom_max_capacity_validation_filter', 20, 2 );
   
       function custom_max_capacity_validation_filter( $result, $tag, $adults, $children, $babies, $pets ) {
       	$tag = new WPCF7_FormTag( $tag );
       	$adults=$_POST['r1-adults'];
       	$children=$_POST['r1-children'];
       	$babies=$_POST['r1-babies'];
       	$pets=$_POST['r1-pets'];
       	$sum=( $adults+$children+$babies+$pets);
       	return $sum;
       	if ($sum > "4" ) {
       		$result->invalidate( $tag, "Rooms cannot accomodate more than 4 people/pets" );
       	}
       	return $result;
       }
       ```
   
 * It does not work, and I cannot find a way to solve the syntax.
    Can you help 
   me, please? I would really appreciate it!

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

 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/sum-validation/#post-8541628)
 * See [Custom Validation](http://contactform7.com/2015/03/28/custom-validation/)
 *  Thread Starter [lillinoki](https://wordpress.org/support/users/lillinoki/)
 * (@lillinoki)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/sum-validation/#post-8543047)
 * Hi Takayuki,
 * thanks for you reply. I did get the idea from that code, but I could not make
   it works…
    My last attempt was:
 *     ```
       add_filter( 'wpcf7_max_capacity*', 'custom_max_capacity_validation_filter', 20, 2 );
   
       function custom_max_capacity_validation_filter( $result, $tag, $zero, $adults, $children, $babies, $pets ) {
       	$tag = new WPCF7_FormTag( $tag );
       	$zero = "0";
       	if ( 'r1-adults' == $tag->name AND 'r1-children' == $tag->name AND 'r1-babies' == $tag->name AND 'r1-pets' == $tag->name ) {
       	$adults = isset( $_POST['r1-adults'] ) ? settype(trim( $_POST['r1-adults'] ), "integer") : settype($zero, "integer");
       	$children = isset( $_POST['r1-children'] ) ? settype(trim( $_POST['r1-children'] ), "integer") : settype($zero, "integer");
       	$babies = isset( $_POST['r1-babies'] ) ? settype(trim( $_POST['r1-babies'] ), "integer") : settype($zero, "integer");
       	$pets = isset( $_POST['r1-pets'] ) ? settype(trim( $_POST['r1-pets'] ), "integer") : settype($zero, "integer");	
       	$sum=$adults+$children+$babies+$pets;
       		if ($sum > 4 ) {
       			$result->invalidate( $tag, "greater than 4" );
       		}
       	}
       	return $result;
       }
       ```
   
 * I think the problem is in the condition:
    if ( ‘r1-adults’ == $tag->name AND ‘
   r1-children’ == $tag->name AND ‘r1-babies’ == $tag->name AND ‘r1-pets’ == $tag-
   >name ) {
 * which in your Custom Validation, would correspond to:
    if ( ‘your-email-confirm’
   == $tag->name ) {
 * but I do not know how to define it…
 * Any further suggestion?
 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/sum-validation/#post-8545998)
 * Please read the post linked from my previous comment.
 *  Thread Starter [lillinoki](https://wordpress.org/support/users/lillinoki/)
 * (@lillinoki)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/sum-validation/#post-8581103)
 * In contact-form-7/includes/functions.php:
 *     ```
       add_filter( 'wpcf7_validate_text', 'custom_ROOM1SUM_validation_filter', 20, 2 );
   
       function custom_ROOM1SUM_validation_filter( $result, $tag ) {
       	$tag = new WPCF7_Shortcode( $tag );
       	$zero = "0";
   
           if ( 'R1-SUM' == $tag->name ) {
               $adults = isset( $_POST['r1-adults'] ) ? $_POST['r1-adults'] : $zero;
       	settype( $adults, "integer");
       	$children = isset( $_POST['r1-children'] ) ? $_POST['r1-children'] : $zero;
       	settype( $children, "integer");
       	$babies = isset( $_POST['r1-babies'] ) ? $_POST['r1-babies'] : $zero;
       	settype( $babies, "integer");
       	$pets = isset( $_POST['r1-pets'] ) ? $_POST['r1-pets'] : $zero;
       	settype( $pets, "integer");
   
               if ( ($adults+$children+$babies+$pets) > 4) {
                   $result->invalidate( $tag, "No more than 4 occupants per room." );
               }
           }
           return $result;
       }
       ```
   
 * In CSS:
 *     ```
       #zero_height {
       	border: 0;
       	height: 0;
       	line-height: 0;
       	padding: 0;
       }
       ```
   
 * In the contact form:
 *     ```
       <label> Adults*
       [select* r1-adults include_blank "1" "2" "3" "4"] </label>
   
       <label> Children (4-12 years)
       [select* r1-children "0" "1" "2" "3"] </label>
   
       <label>  Babies (0-3 years)
       [select* r1-babies "0" "1" "2" "3"] </label>
   
       <label> Small and medium sized pets
       [select* r1-pets "0" "1" "2" "3"] </label>
       [text R1-SUM id:zero_height]
       ```
   
 *  [TexWiller](https://wordpress.org/support/users/texwiller/)
 * (@texwiller)
 * [9 years ago](https://wordpress.org/support/topic/sum-validation/#post-8905824)
 * Hello,
 * Thanks for the code. How do I modify it so that the dropdown has descriptions
   and values?
 * Example of a dropdown I’m trying to create:
 * Q: Are you older that 21?
    A: Yes – would have a value of 10 and No would be 
   zero.
 * In the function I would process the number and not the “Yes” or “No”.
 * Thanks!

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

The topic ‘Sum validation’ 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/)

## Tags

 * [input](https://wordpress.org/support/topic-tag/input/)
 * [sum](https://wordpress.org/support/topic-tag/sum/)
 * [validate](https://wordpress.org/support/topic-tag/validate/)
 * [validation](https://wordpress.org/support/topic-tag/validation/)

 * 5 replies
 * 3 participants
 * Last reply from: [TexWiller](https://wordpress.org/support/users/texwiller/)
 * Last activity: [9 years ago](https://wordpress.org/support/topic/sum-validation/#post-8905824)
 * Status: resolved