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.

Limit date range

  1. I've seen lots of people asking about how to limit the date field to only allow certain dates (for example not being about to select dates in the past).

    You (the support staff) always point the toward the jquery datepicker.

    After a bit of work, I did manage to get the datepicker to limit certain dates, but this doesn't actually limit the dates that are submitted to the form. In this example, a user can simply key in a different date without using the datepicker graphic interface. This allows them to get past the limitation.

    I bet this can be fixed with some custom php, but my bigger point is that you shouldn't be telling people that this can be achieved with datepicker alone.

    Unless I'm missing something... And I HOPE I'm missing something because I don't want to have to deal with this another way...

    So, am I missing something?

    Posted 12 years ago on Thursday October 20, 2011 | Permalink
  2. We simply stated that the datepicker element is part of the jQuery UI library and it can be customized using a variety of parameters passed to the script. As far as "you shouldn't be telling people that this can be achieved with datepicker alone" I'm not aware that anyone has said that or guaranteed it to be true.

    If you want to customize the datepicker element, you can use some custom jQuery to do set it up. If you want to limit what the input itself accepts, then you would have to write your own custom validation for the field using the gform_validation hook.

    http://www.gravityhelp.com/documentation/page/Using_the_Gravity_Forms_%22gform_validation%22_Hook

    So, no, you're not missing anything. We do plan on adding things like a date range field in the future and even adding more options to the existing datepicker field in the admin. For now, you'll have to write something custom to get it done.

    Posted 12 years ago on Friday October 21, 2011 | Permalink
  3. That link is an excellent walk-through that I had not yet read.

    Thanks.

    Posted 12 years ago on Monday October 24, 2011 | Permalink
  4. Please let us know if you need help implementing something.

    Posted 12 years ago on Monday October 24, 2011 | Permalink
  5. Hey I've been reading through the gform_validation_hook tutorial repeatedly and I just can't seem to make the connection from the example given to the goal that I have in mind.

    I basically want to create a birth date validator that would suspend the registration process if the supplied birthday doesn't add up to the age of 21 (on December 8, 2011).

    Here's the link to my form: http://motormediausa.com/test/

    And here's my code:

    // CUSTOM AGE VERIFICATION
    add_filter( 'gform_validation_1', 'age_validation');
    function age_validation($validation_result){
    
    	$form = $validation_result['form'];
    	foreach($form['fields'] as &$field){
    		// 5 - If the field does not have our designated CSS class, skip it
            if(strpos($field['cssClass'], 'validate-age') === false)
                continue;
    
    			// 7 - Check if the field is hidden by GF conditional logic
    			$is_hidden = RGFormsModel::is_field_hidden($form, $field, array());
    
    			// 8 - If the field is not on the current page OR if the field is hidden, skip it
    			if($field_page != $current_page || $is_hidden)
    			continue;
    
    			// 9 - Get the submitted value from the $_POST
    			$field_value = rgpost("input_{$field['id']}");
    
    			// 10 - Make a call to your validation function to validate the value
    			$is_valid = is_age($field_value);
    
    			// 11 - If the field is valid we don't need to do anything, skip it
    			if($is_valid)
    				continue;
    
    			// 12 - The field field validation, so first we'll need to fail the validation for the entire form
    			$validation_result['is_valid'] = false;
    
    			// 13 - Next we'll mark the specific field that failed and add a custom validation message
    			$field['failed_validation'] = true;
    			$field['validation_message'] = 'The birthday you have entered indicates that you are not of legal drinking age and will be ineligible to compete in this tournament.';
    
        }
    
    	// 14 - Assign our modified $form object back to the validation result
        $validation_result['form'] = $form;
    
        // 15 - Return the validation result
        return $validation_result;
    }
    
    function is_age($age) {
    		$day = ("#input_1_3_2");
    		$month = ("#input_1_3_1");
    		$year = ("#input_1_3_3");
    		$age = 21;
    
    	$mydate=new Date();
    	mydate.setFullYear(1990,11-1,8);
    
    	$currdate = new Date();
    	currdate.setFullYear(currdate.getFullYear() - age);
    	if ((currdate - mydate) < 0){
    	alert("Sorry, only persons over the age of " + age + " may enter this competition.");
    	return false;
    	}
    	return true;
    	};

    Any help that you could provide would be excellent. Thank you.

    Posted 12 years ago on Tuesday November 8, 2011 | Permalink
  6. What happens when you run that code? And can we see this in action somewhere? Thank you.

    Posted 12 years ago on Tuesday November 8, 2011 | Permalink
  7. Hey! Thank you for the quick response. When I click the submit button, I get a

    Fatal error: Class 'Date' not found in /home/content/i/n/d/indiangig/html/wp-content/themes/motormediausa/functions.php on line 375

    As I said, I've been trying to make the connection between the VIN# validation script and what I'm attempting to do here and just getting nowhere with it.

    Posted 12 years ago on Tuesday November 8, 2011 | Permalink
  8. Please post your functions.php at pastie.org or pastebin.com. The error is on line 375 in that functions.php file. I'd like to see if you have the name of a function wrong or something. Take a look at that file, or post the content so we can see it ourselves. Thank you.

    Posted 12 years ago on Tuesday November 8, 2011 | Permalink
  9. Has There been a solution to this thread? We are trying the same thing over here.

    Thank you in advance.

    Posted 11 years ago on Monday May 21, 2012 | Permalink
  10. I think in line 54 & 56, 'age' is missing the leading '$' for a php variable- should be $age in the is_age() function- could be part of the issue?

    Posted 11 years ago on Sunday June 3, 2012 | Permalink
  11. +1.
    I'm trying to do a similar thing (I have date fields that I want to check against the current date to check if the user is of legal age). I scoured the forums and the docs, but cannot find the proper way to reference multiple subfields. Before I get sent to the validation hooks tutorial, all that is stated there is that "we will discuss multiple inputs at a future time".

    Can you *please* let us know how to do this? I've tried variations on the is_age function above with no success.

    Posted 11 years ago on Thursday July 26, 2012 | Permalink
  12. redjelly, please begin a new topic for your issue and explain clearly what you would like to do. We'll be happy to help. Reference this topic if it's relevant. Thank you.

    Posted 11 years ago on Thursday July 26, 2012 | Permalink

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