I would like to know if there is a simple way to find the age of a person based on DOB entered and then display it on the form.

Example:
DOB: 12/27/1997 Age is: 18

Comments

Vako created an issue. See original summary.

Vako’s picture

Issue summary: View changes
Subhransu.addweb’s picture

For that you need to alter webform using hook_form_alter(&$form, &$form_state, $form_id) and apply below code for your fields( i.e. dob,age).

                $month = $form_state['input']['submitted']['dob']['month'];
                $day = $form_state['input']['submitted']['dob']['day'];
                $year = $form_state['input']['submitted']['dob']['year'];
                
                $date = new DateTime();
                if(!empty($year))
                $date = new DateTime("$year-$month-$day 00:00:00");
                $now = new DateTime();
                $interval=$date->diff($now);
                $form_state['input']['submitted']['age'] = $interval->y ;

Feel free to contact me incase of any query/concern regarding this.

Thanks!

Vako’s picture

Thank you Denisha for the reply. Can you please elaborate a little more as where I can put such a code and how I can have it displayed beside the DOB field.

Subhransu.addweb’s picture

For you purpose you require to place that calculation in js code. You can add the below code in your js file. Take two fields (dob, age).

jQuery( document ).ready(function( $ ) {        
        jQuery('.webform-component--dob').change(function($){
                month = jQuery('#edit-submitted-bod-month option:selected').text(); // Take your field value
                day = jQuery('#edit-submitted-bod-day option:selected').text(); // Take your field value
                year = jQuery('#edit-submitted-bod-year option:selected').text(); // Take your field value
                //alert(month+'month'+day+'day'+year+'year');
                if(jQuery(month.length != 0) && jQuery(day.length != 0)  && jQuery(year.length != 0)){
                        var today = new Date();
                        var yyyy = Number(today.getFullYear()); 
                        age = yyyy - year; 
                        jQuery('#edit-submitted-age').val(age); // Set  your age field value.
                }        
        });        
});

Feel free to contact me in case of any query/concern regarding the same.

Thanks!

ashhishhh’s picture

Issue summary: View changes
Vako’s picture

Thank you Denisha. The js file that you mentioned, is it the \sites\all\modules\webform\js\webform.js file?
and how can I control the location of the Age display?
I will try it and let you know how it works.

Subhransu.addweb’s picture

Nop Vako! Not there, please place it in your theme js file (i.e./sites/all/themes/yourtheme/js/custom.js).

.webform-component--dob:- Please place your dob here.

Hope this helps you.

Vako’s picture

I did insert the above code in the theme's custom.js file, replaced .webform-component--dob with .webform-component--applicant_dob
But where will the Age value be visible, or how can I control the location of the Age value?

In your code there are #edit-submitted-bod-### values, shouldn't it be #edit-submitted-dob instead?
I believe we need to replace those with the webform equivalent as well?

Liam Morland’s picture

Status: Active » Fixed

Over a year old; re-open if you still need help.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.